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