SpringBoot整合JSP
- 创建webapp项目


- 导入项目依赖jar包
<!-- Spring Boot父级依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <!-- web开发支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 将tomcat 内嵌到 web项目中,这样可以直接运行 webapp项目。不需要再部署到额外的tomcat --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <!-- 引入SpringBoot内嵌Tomcat对jsp的解析依赖,不添加这个解析不了jsp --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- 简化JSP开发,不用嵌入Java代码就能够开发出复杂的JSP页面 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
- 创建配置文件application.yml。配置文件放在resources文件夹下。

server:
# 配置端口号
port: 8088
spring:
mvc:
view:
# 配置视图前缀。eg:/view/
prefix: /
# view视图文件的后缀
suffix: .jsp
- 创建控制器
package com.exambner.springboot.jsp.handler; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/hello") public class HelloHandler { @GetMapping("/index") public String index() { return "index"; } }
- 项目启动项
package com.exambner.springboot.jsp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ExambnerApplication { public static void main(String[] args) { SpringApplication.run(ExambnerApplication.class, args); } }

- 实现JSP增删改查功能
添加实体类Entity。
package com.exambner.springboot.jsp.entity; import org.springframework.stereotype.Component; @Component public class Student { /** * id */ private Long id; /** * 姓名 */ private String name; /** * 年龄 */ private int age; /** * 性别 */ private String sex; public Student() { } public Student(Long id, String name, int age, String sex) { this.id = id; this.name = name; this.age = age; this.sex = sex; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + '}'; } }
添加数据访问层Repository。
package com.exambner.springboot.jsp.repository; import com.exambner.springboot.jsp.entity.Student; import org.springframework.stereotype.Repository; import java.util.Collection; import java.util.HashMap; import java.util.Map; @Repository public class StudentRepository { /** * 存储学生信息,模拟数据库 */ private static Map<Long, Student> map; static { map = new HashMap<>(); map.put(1L, new Student(1L, "小明", 18, "男")); map.put(2L, new Student(2L, "小红", 16, "女")); map.put(3L, new Student(3L, "小白", 21, "男")); } /** * 获取全部学生信息 * * @return */ public Collection<Student> getAll() { return map.values(); } /** * 获取学生信息 * * @param id * @return */ public Student getStudentById(Long id) { return map.get(id); } /** * 保存获取更新学生信息 * * @param student */ public void saveOrUpdateStudent(Student student) { map.put(student.getId(), student); } /** * 删除学生信息 * * @param id */ public void deleteStudent(Long id) { map.remove(id); } }
添加业务层Service。
package com.exambner.springboot.jsp.service; import com.exambner.springboot.jsp.entity.Student; import java.util.Collection; /** * 学生业务接口 */ public interface IStudentService { /** * 获取全部学生信息 * * @return */ public Collection<Student> getAll(); /** * 获取学生信息 * * @param id * @return */ public Student getStudentById(Long id); /** * 保存获取更新学生信息 * * @param student */ public void saveOrUpdateStudent(Student student); /** * 删除学生信息 * * @param id */ public void deleteStudent(Long id); }
package com.exambner.springboot.jsp.service.impl; import com.exambner.springboot.jsp.entity.Student; import com.exambner.springboot.jsp.repository.StudentRepository; import com.exambner.springboot.jsp.service.IStudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Collection; /** * 学生业务实现层 */ @Service public class StudentServiceImpl implements IStudentService { @Autowired private StudentRepository studentRepository; @Override public Collection<Student> getAll() { return studentRepository.getAll(); } @Override public Student getStudentById(Long id) { return studentRepository.getStudentById(id); } @Override public void saveOrUpdateStudent(Student student) { studentRepository.saveOrUpdateStudent(student); } @Override public void deleteStudent(Long id) { studentRepository.deleteStudent(id); } }
添加控制器层Handler。
package com.exambner.springboot.jsp.handler; import com.exambner.springboot.jsp.entity.Student; import com.exambner.springboot.jsp.service.IStudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/student") public class StudentHandler { @Autowired private IStudentService studentService; /** * 获取全部学生信息 * * @return */ @GetMapping("/index") public ModelAndView getAll() { //创建视图模型 ModelAndView modelAndView = new ModelAndView(); //指定视图跳转页面 modelAndView.setViewName("index"); //向视图中添加学生信息 modelAndView.addObject("list", studentService.getAll()); return modelAndView; } }
新增JSP页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%-- isELIgnored:控制JSP是否使用忽略EL表达式。 true:表示不使用EL表达式,则传入的表达式会被识别为字符串,原样输出。例如: 传入{100 % 10},输出为{100 % 10} false:表示使用EL表达式,则传入的表达式会被执行计算。例如: 传入{100 % 10},输出为0 --%> <%@ page isELIgnored="false" %> <%-- 引用JSTL核心标签库 --%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>学生信息</title> </head> <body> <h2>学生信息</h2> <a href="/add.jsp" >新增学生</a> <table> <tr> <td>学生编号</td> <td>学生姓名</td> <td>学生年龄</td> <td>学生性别</td> <td>操作</td> </tr> <c:forEach items="${list}" var="student"> <tr> <td>${student.id}</td> <td>${student.name}</td> <td>${student.age}</td> <td>${student.sex}</td> <td> <a href="/student/getStudentById/${student.id}">更新</a> <a href="/student/delete/${student.id}">删除</a> </td> </tr> </c:forEach> </table> </body> </html>
访问学生信息。 访问地址:http://localhost:8088/student/index。

删除学生信息
/** * 删除学生信息 * * @param id * @return */ @GetMapping("/delete/{id}") public String deleteStudentById(@PathVariable("id") Long id) { //删除学生信息 studentService.deleteStudent(id); //重定向到index接口,重新获取数据,刷新页面 return "redirect:/student/index"; }
保存学生信息
- JSP页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>添加学生信息</title> <form action="/student/save" method="post"> 学生编号:<input type="text" name="id"><br> 学生姓名:<input type="text" name="name"><br> 学生年龄:<input type="text" name="age"><br> 学生性别:<input type="text" name="sex"> <input type="submit" value="提交"> </form> </head> <body> </body> </html>
- 控制器接口
/** * 保存学生信息 * * @param student * @return */ @PostMapping("/save") public String saveStudent(Student student) { //保存学生信息 studentService.saveOrUpdateStudent(student); //重定向到index接口,重新获取数据,刷新页面 return "redirect:/student/index"; }
更新学生信息
- JSP页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>更新学生信息</title> <form action="/student/update" method="post"> 学生编号:<input type="text" name="id" value="${student.id}" readonly><br> 学生姓名:<input type="text" name="name" value="${student.name}"><br> 学生年龄:<input type="text" name="age" value="${student.age}"><br> 学生性别:<input type="text" name="sex" value="${student.sex}"> <input type="submit" value="提交"> </form> </head> <body> </body> </html>
- 控制器接口
/** * 更新学生信息 * * @param student * @return */ @PostMapping("/update") public String updateStudent(Student student) { //更新学生信息 studentService.saveOrUpdateStudent(student); //重定向到index接口,重新获取数据,刷新页面 return "redirect:/student/index"; }

浙公网安备 33010602011771号