thymeleaf模板引擎文件就是一個普通的html靜態文件,既能夠做爲靜態資源訪問,查看模板樣例;又能夠mvc方式訪問,變爲動態內容頁面html
mavenweb
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
複製代碼
application.propertiesspring
# 整合thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
# 路徑前綴
spring.thymeleaf.prefix=classpath:/templates/tmlf/
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.suffix=.html
# 配置靜態資源文件夾
# 添加自定義靜態資源文件夾 ,classpath:/js/,classpath:/test/,classpath:/templates/
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/js/,classpath:/test/,classpath:/templates/
複製代碼
index.htmlbash
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>thymeleaf模板引擎</title>
</head>
<body>
姓名:<span th:text="${userInfo.name}">張三(模板)</span> <br>
地址:<span th:text="${userInfo.address}">成都(模板)</span>
</body>
</html>
複製代碼
ThymeleafControllermvc
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.demo.dto.UserInfo;
@Controller
@RequestMapping("tmlf")
public class ThymeleafController {
@RequestMapping("index")
public String index(ModelMap modelMap) {
modelMap.put("name", "freemarker");
UserInfo userInfo=new UserInfo("張三", "綿陽");
modelMap.addAttribute("userInfo", userInfo);
return "index";
}
}
複製代碼
MVC模式訪問:
http://localhost:8080/tmlf/indexapp