我们在上篇文章 https://blog.csdn.net/whandgdh/article/details/95937078
spring boot 整合freemarker实例基础上修改 整合thymeleaf。
step1 pom依赖
<!--spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--thymeleaf 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
step2 application.yml配置文件
spring:
thymeleaf:
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8
servlet:
content-type: text/html
enabled: true
mode: HTML5
cache: false
看到配置文件访问的页面类型是html
step3 HelloWorldController 类 代码
@RequestMapping("/helloByMv")
public ModelAndView helloByMv(){
ModelAndView model=new ModelAndView("hello") ;
Map<String,String>map=new HashMap<String,String>();
map.put("msg", "Hello thymeleaf!");
model.addAllObjects(map);
return model;
}
@RequestMapping("/helloToThy")
public String helloByMv(ModelMap map ){
map.addAttribute("msg", "Hello thymeleaf!");
return "hello";
}
step4 新建 hello.html 页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 th:text="${msg}"> </h1>
</body>
</html>
step 5 启动工程访问
访问路径
http://127.0.0.1:8080/helloByMv
http://127.0.0.1:8080/helloToThy