- 创建springboot项目
记得勾选web
2.在pom.xml中添加Thymeleaf依赖
<!--thymeleaf模板依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3. 在application.properties修改Thymeleaf相关设置
#路径
spring.thymeleaf.prefix=classpath:/templates/
#后缀
spring.thymeleaf.suffix=.html
#编码
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
4. 编写controller
@Controller
public class TestController {
@RequestMapping("/test")
public ModelAndView test(){
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("hello");
List<String> list=new ArrayList<String>();
list.add("zhangsan");
list.add("lisi");
list.add("wangwu");
modelAndView.addObject("list",list);
return modelAndView;
}
}
5. 在src/main/resource/templates下编写hello.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"> <!--需要添加此行标注为thymeleaf模板 -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1px">
<tr>
<th>name</th>
</tr>
<tr th:each="name:${list}">
<td th:text="${name}"></td>
</tr>
</table>
</body>
</html>
6.测试