1.修改pom文件,添加坐标
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- jasper --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
2. 创建Controller
@Controller public class UserController { @RequestMapping("/showUser") public String showUser(Model model) { List<Users> list = new ArrayList<>(); list.add(new Users(1,"张三",20)); list.add(new Users(2,"李四",22)); list.add(new Users(3,"王五",24)); model.addAttribute("list", list); return "userList"; }
3.建立jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>SpringBoot 整合JSP</title> </head> <body> <table border="1" align="center" width="50%"> <tr> <th>ID</th> <th>Name</th> <th>Age</th> </tr> <c:forEach items="${list}" var="user"> <tr> <td>${user.id }</td> <td>${user.name }</td> <td>${user.age }</td> </tr> </c:forEach> </table> </body> </html>
4.修改application.properties配置文件
#prefix : 是指访问页面的前缀,指定页面存放的文件夹 spring.mvc.view.prefix=/WEB-INF/jsp/ # suffix : 是指文件的后缀名,常见的后缀名有html,jsp,php,txt,mp3 spring.mvc.view.suffix=.jsp