Thymeleaf引入
Thymeleaf是一个Java模板引擎开发库,可以处理和生成HTML、XML、JavaScript、CSS和文本,在Web和非Web环境下都可以正常工作。
1.添加依赖包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2.添加配置项目
# thymeleaf spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html # 开发阶段务必关闭缓存 (=false) spring.thymeleaf.cache=false
3.添加
@RequestMapping("/list") public String listUser(Model model) { List<User> userList = new ArrayList<User>(); for (int i = 0; i <10; i++) { userList.add(new User(i,"张三"+i,20+i,"中国广州")); } model.addAttribute("users", userList); return "/user/list"; }