1. 程式人生 > >Spring Boot thymeleaf模版支援,css,js等靜態檔案新增

Spring Boot thymeleaf模版支援,css,js等靜態檔案新增

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"; }