一、添加freemarker相关依赖
pom文件中添加以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
二、添加freemarker配置
在application.properties文件中加入以下配置
# 是否开启thymeleaf缓存,本地为false,生产建议为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.allow-request-override=false
spring.freemarker.check-template-location=true
#类型
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
#文件后缀
spring.freemarker.suffix=.ftl
#路径
spring.freemarker.template-loader-path=classpath:/templates/
三、编写页面
在templates下创建文件加user,在user下添加页面user.ftl:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello World!
<h1>hello world!</h1>
<!-- 获取controller传过来的值 -->
<h1> ${user.name} </h1>
<h1> ${user.age} </h1>
</body>
</html>
四、编写测试用的controller
@Controller
@RequestMapping("/user")
public class UserController {
@GetMapping(value = "/index")
public String index(ModelMap modelMap){
//User为一个实体类,有name和age两个属性
User user = new User();
user.setName("mw_monster");
user.setAge(24);
modelMap.addAttribute("user",user);
return "user/user";
}
}
五、启动项目,进行测试
启动项目,浏览器中访问http://localhost:8080/user/index,能进入到页面并且不报错,整合成功并测试成功。