SpringBoot集成Thymeleaf模板
1,pom.xml,引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2,application.properties配置
#Thymeleaf配置
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=classpath:/templates
spring.thymeleaf.suffix=.html
2,文件目录
3,controller
package alu.controller; import javax.servlet.http.HttpServletRequest; import org.apache.ibatis.annotations.Delete; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.github.pagehelper.PageHelper; import alu.bean.User; import alu.service.TestService; import alu.util.JwtUtils; import io.jsonwebtoken.Claims; @Controller @RequestMapping("/user") public class TestController { @Autowired private TestService test; @RequestMapping(value = "/index") public String index(HttpServletRequest request){ PageHelper.startPage(0, 10); request.setAttribute("test", test.findAllVideo()); return "/user/index"; } @RequestMapping(value = "/indexT") public String isndex(HttpServletRequest request){ request.setAttribute("test", "asdasda"); return "/index"; } }
4,html存放位置
5,user里的index页面
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<!--静态资源这样引用是成功了的 ,下面的图是之前没引入成功截的,懒的改了-->
<link th:href="@{/res/bootstrap/css/bootstrap.min.css}" rel="stylesheet">
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<script th:src="@{/res/bootstrap/js/jquery.min.js}" ></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script th:src="@{/res/bootstrap/js/bootstrap.min.js}" ></script>
<title>index</title> </head> <body> <div class="row"> <div class="col-md-5"> <table class="table table-hover" id="tab"> <thead> <tr> <th>id</th> <th>title</th> <th>summary</th> <th>操作</th> </tr> </thead> <tbody> <tr th:each="ItemList : ${test}"> <td th:text="${ItemList.id }"></td> <td th:text="${ItemList.title }"></td> <td th:text="${ItemList.summary }"></td> </tr> </tbody> </table> </div> </div> </body> </html>
效果: