Thymeleaf引入
Thymeleaf是一個Java模板引擎開發庫,可以處理和生成HTML、XML、JavaScript、CSS和文本,在Web和非Web環境下都可以正常工作。
1.添加依賴包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.添加配置項目
# 模板配置 # 這個開發配置為false,避免改了模板還要重啟服務器 spring.thymeleaf.cache=false # 這個是配置模板路徑的,默認就是templates,可不用配置 spring.thymeleaf.prefix=classpath:/templates/ # 這個可以不配置,檢查模板位置 spring.thymeleaf.check-template-location=true # 下面3個不做解釋了,可以不配置 spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html # 模板的模式 spring.thymeleaf.mode=HTML5
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";
}
