一、SpringBoot整合jsp
在maven的dependencies的依赖中除了springBoot启动器还要添加对jstl和jsp的依赖。
1 <dependencies>
2 <dependency>
3 <groupId>org.springframework.boot</groupId>
4 <artifactId>spring-boot-starter-web</artifactId>
5 </dependendy>
6 <dependency>
7 <groupId>javax.servlet</groupId>
8 <artifactId>jstl</artifactId>
9 </dependency>
10 <dependency>
11 <groupId>org.apache.tomcat.embed</groupId>
12 <artifactId>tomcat-embed-jasper</artifactId>
13 <scope>provided</scope>
14 </dependency>
15 </dependencies>
在application.properties中修改jsp全局访问设置
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
接下来就只需要在controller中返回spring类型的视图名称即可访问到WEB-INF/jsp/中相应的jsp文件
书写controller
1 @Controller 2 public class UserController { 3 @RequestMapping("/showUser") 4 public String showUser(Model model) { 5 List<User> userList = new ArrayList<>(); 6 userList.add(new User("鬼魅传说")); 7 userList.add(new User("魑魅魍魉")); 8 userList.add(new User("妖魔")); 9 model.addAttribute("userList", userList); 10 return users; 11 } 12 }
书写WEB-INF/jsp/users.jsp
1 <%@page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
2 <%@tagbib url="http://java.sun.com/jsp/jstl/core" prefix="c" %>
3 <html>
4 <head>
5 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
6 <title></title>
7 </head>
8 <body>
9 <table border="1" align="center" width="50%">
10 <tr>
11 <th>id</th>
12 <th>name</th>
13 <th>age</th>
14 </tr>
15 <c:forEach items="${userList}" var="user">
16 <tr>
17 <td>${user.id}</td>
18 <td>${user.name}</td>
19 <td>${user.age}</td>
20 </tr>
21 </c:forEach>
22 </table>
23 </body>
24 </html>
二、SpringBoot整合Freemarker
在maven中除了添加springboot启动器外,还要添加对freemarker的依赖
1 <dependencies>
2 <dependency>
3 <groupId>org.springframework.boot<groupId>
4 <artifactId>spring-boot-starter-web</artifactId>
5 </dependency>
6 <dependency>
7 <groupId>org.springframework.boot</groupId>
8 <artifactId>spring-boot-starter-freemarker</artifactId>
9 </dependency>
10 </dependencies>
springBoot要求模板形式的视图层技术的文件必须放到src/main/resources目录下必须要有一个名称为templates的文件夹
编写controller,同上也是把userList放到model的attribute中即可
然后写freemarker模板即可
1 <html>
2 <head>
3 <meta chaset="utf-8">
4 </head>
5 <body>
6 <table border="1" align="center" width="50%">
7 <tr>
8 <td>id</td>
9 <td>name</td>
10 <td>age</td>
11 </tr>
12 <#list userList as user>
13 <tr>
14 <td>${user.id}</td>
15 <td>${user.name}</td>
16 <td>${user.age}</td>
17 </tr>
18 </#list>
19 </table>
20 </body>
21 </html>
三、springboot整合Thymeleaf
在maven中添加对thymeleaf的依赖。
1 <dependencies>
2 <dependency>
3 <groupId>org.springframework.boot</groupId>
4 <artifactId>spring-boot-starter-web</artifactId>
5 </dependency>
6 <dependency>
7 <groupId>org.springframework.boot</groupId>
8 <artifactId>spring-boot-starter-thymeleaf</artifactId>
9 </dependency>
10 </dependecies>
视图存放的位置为src/main/resources/templates,该目录是安全的,意味着该目录下的内容不允许被外界直接访问。
编写controller同上,在model中添加attributeute的userList即可
编写前端视图src/main/resources/templates/users.html
1 <html>
2 <head>
3 <meta charset="utf-8">
4 </head>
5 <body>
6 <table>
7 <tr>
8 <td>id</td>
9 <td>name</td>
10 <td>age</td>
11 </tr>
12 <tr th:each="user : ${userList}">
13 <td th:text="${user.id}"></td>
14 <td th:text="${user.name}"></td>
15 <td th:text="${user.age}"></td>
16 </tr>
17 </table>
18 </body>
19 </html>