在pom文件导入需要的依赖
<!--整合freemarker-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!--整合thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在配置文件application.yml (本人喜欢yml的布局,只要导入了包就会有友好的代码提示 也可以用properties,看个人习惯)
#设置freemarker参数
spring:
freemarker:
cache: false
#整和thymeleaf
thymeleaf:
cache: false
这里主要是是关闭缓存方便调试 ,线上环境直接关闭就可以了
在controller里面
@RequestMapping("/fre")
public ModelAndView test() {
ModelAndView m =new ModelAndView("index1");
m.addObject("user","gcr");
return m;
}
@RequestMapping("/thy")
public ModelAndView thymeleaf(Model model) {
ModelAndView m =new ModelAndView("index2");
m.addObject("user","gcr");
return m;
}
freemarker和thymeleaf 都在spring boot 创建项目时生成的文件夹templates下面 thymeleaf 以html 结尾 freemarker以ftl结尾
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>index1.html</title>
</head>
<body>
<div>
<p th:text="${user}">default message</p>
hello thymeleaf
</div>
</body>
</html>
以上是thymeleaf
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p>欢迎${user}</p>
this is a freemarker
</body>
</html>
以上是freemarker
启动项目直接运行发送请求
网友评论