Spring Boot入门2——Spring Boot整合JSP
在入门1中学习了一个简单的maven项目是如何搭建的,但是只能够直接对数据进行操作,这在我们的开发中是远远不够的。所以我们还需要将Spring Boot将JSP整合到一起使项目给用户更好的用户体验。
- 生成一个maven项目,配置文件pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
</parent>
<dependencies>
<!-- web相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jsp相关依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- JSTL依赖 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
- 创建配置文件application.yml
server:
port: 8080
spring:
mvc:
view:
prefix: /
surfix: .jsp
-
将入门1中的entity包、repository包和Application启动类复制到当前的项目中
-
创建Handler(Controller)
import com.southwind.entity.Student;
import com.southwind.repository.StudentRepository;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/hello")
public class HelloHandler {
@Autowired
private StudentRepository studentRepository;
@RequestMapping("/index")
public ModelAndView index() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
modelAndView.addObject("list", studentRepository.findAll());
return modelAndView;
}
@GetMapping("/deleteById/{id}")
public String deleteById(@PathVariable long id) {
studentRepository.deleteById(id);
return "redirect:/hello/index";
}
@PostMapping("/save")
public String save(Student student) {
studentRepository.saveOrUpdate(student);
return "redirect:/hello/index";
}
@PostMapping("/update")
public String update(Student student) {
studentRepository.saveOrUpdate(student);
return "redirect:/hello/index";
}
@GetMapping("/findById/{id}")
public ModelAndView findById(@PathVariable long id) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("update");
modelAndView.addObject("student", studentRepository.findById(id));
studentRepository.findById(id);
return modelAndView;
}
}
- jsp
- index.jsp【主页面】
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!-- 使用EL表达式 -->
<%@ page isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>学生信息</h1>
<table border="1" bordercolor="black" style="border-collapse:collapse;">
<tr>
<th>学生编号</th>
<th>学生姓名</th>
<th>学生年龄</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="student">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>
<a href="/hello/findById/${student.id}">修改</a>
<a href="/hello/deleteById/${student.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<a href="/save.jsp">添加学生</a>
</body>
</html>
- save.jsp【增加学生页面】
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/hello/save" method="post">
ID:<input type="text" name="id"></br>
name:<input type="text" name="name"></br>
age:<input type="text" name="age"></br>
<input type="submit" value="提交">
</form>
</body>
</html>
- update.jsp【更新信息页面】
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/hello/update" method="post">
ID:<input type="text" name="id" value="${student.id}" readonly></br>
name:<input type="text" name="name" value="${student.name}"></br>
age:<input type="text" name="age" value="${student.age}"></br>
<input type="submit" value="提交">
</form>
</body>
</html>
目录结构
效果
- 启动项目后在浏览器中输入
http://localhost:8080/hello/index
- 添加学生
- 修改学生信息
- 删除学生
总结
使用Spring Boot完成了一个小项目,最大的感受是不用像使用SSM框架时要写多个繁琐的配置文件,仅需要进行简单的配置就能实现相同的效果。在业务逻辑的实现上与使用SSM框架开发时并没有不同。