SpringBoot視圖層技術


一、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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM