一,整合freemarker
1,启动类
@SpringBootApplication(scanBasePackages= {"com.*.controller","com.*.service"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2,application.yml
spring:
freemarker:
allow-request-override: false
cache: true
check-template-location: true
charset: utf-8
content-type: text/html; charset=utf-8
expose-request-attributes: false
expose-session-attributes: false
expose-spring-macro-helpers: false
suffix: .ftl
template-loader-path: ["/WEB-INF/templates","classpath:/templates"] #多个路径
默认会在resources下的templates下寻找
3,controller
@Controller
@RequestMapping("freemarker")
public class FreeMarkerController {
@RequestMapping("freemarker.do")
public String freemarker(Model model) {
model.addAttribute("name", "admin");
return "show";
}
}
4,show.ftl
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<h1>欢迎你 ${name }</h1>
</body>
</html>
5,pom添加整合freemarker依赖
<!-- 整合freemark -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
二,整合thymeleaf
1,application.yml
spring:
thymeleaf:
cache: false #关闭thymeleaf缓存,否则没有实时画面
check-template-location: true #检查模板是否存在,然后在呈现
content-type: text/html; charset=utf-8
enabled: true
mode: LEGACYHTML5
prefix: /WEB-INF/templates/
suffix: .html
2,controller
@Controller
@RequestMapping("thymeleaf")
public class ThymeLeafController {
@RequestMapping("index.do")
public String index(Model model) {
model.addAttribute("name", "admin");
return "thymeleaf";
}
}
4,pom
<!-- 整合thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
5,thymeleaf.html 需要使用thymeleaf标签
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello<h1 th:text="${name}"> </h1>
</body>
</html>