由于想在以前搭建的项目使用(以前使用jsp),注释掉jsp相关之后,反正都不能行得通。还是新建整合吧,idea新建springboot可以自行百度。
首先引入Thymeleaf (创建的时候也可以勾选,我没有)
**
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
**
properties文件配置
spring.thymeleaf.prefix=classpath:/templates/
####后缀
spring.thymeleaf.suffix=.html
#####模板格式
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.cache=false
html文件默认放置在template文件夹
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Thymeleaf模板引擎
<h1 th:text="${name}"></h1>
<table border="1">
<tr>
<th>序号</th>
<th>用户id</th>
<th>用户名</th>
<th>描述</th>
<th>图片</th>
<th>创建时间</th>
<th>状态变量:last</th>
<th>状态变量:size</th>
<th>状态变量:current</th>
</tr>
<tr th:each="user,userStat:${list}">
###可以运算
<th th:text="${userStat.index-5}"></th>
<th th:text="${user.id}">状态变量:current</th>
<th th:text="${user.name}">状态变量:name</th>
<th th:text="${user.detail}">状态变量:odd</th>
<th th:text="${user.pic}">状态变量:first</th>
<th th:text="${user.createtime}">状态变量:last</th>
<th th:text="${userStat.last}">状态变量:last</th>
<th th:text="${userStat.size}">状态变量:size</th>
<th th:text="${userStat.current}">状态变量:current</th>
</tr>
</table>
</body>
</html>
controller层
@RequestMapping("/thymeleaf")
public String index(ModelMap map){
map.addAttribute("name","Thymeleaf测试");
List<ItemsVo> list = mybatisService.selectAll();
map.addAttribute("list",list);
return "index";
}
效果