目录
二、Spring Boot 控制器Controller的配置
SpringBoot并不推荐使用jsp作为页面数据处理技术,而是推荐使用thymeleaf作页面处理。Thymeleaf和FreeMarker一样,都是Java开发常用的模板引擎,但是FreeMarker已经停止维护很久了,且FreeMarker的特长是将页面静态化处理;而Thymeleaf则是一款更先进的模板引擎,它的设计原则是“原型即界面”,而不像FreeMarker一样,要先准备数据,然后再填充数据生成另一个html文件,Thymeleaf是直接支持html文件的。
更多关于SpringBoot的总结请点击:SpringBoot使用总结
一、在build.gradle中添加thymeleaf依赖
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
二、Spring Boot 控制器Controller的配置
需要使用Model来进行参数传递(或者自定义Map)
因为Thymeleaf 默认的视图返回路径是 /src/java/resources/templates ,而且默认后缀是.html , 所以我们的页面视图就放到templates目录下
@RequestMapping("/index")
public String index(Model model) {
model.addAttribute("data", "springboot_thymeleaf");
return "index";
}
三、页面视图的配置
在html标签上引入 Thymeleaf 的标签库,然后参数输出就可以直接使用${} 了
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!-- 标准表达式-->
<div th:text="${data}">
SpringBoot 集成Thymeleaf
</div>
</body>
</html>
四、禁用缓存和去除对HTML5标签的严格校验
在application.properties中配置
#开发阶段,建议关闭thymeleaf的缓存
spring.thymeleaf.cache=false
#使用遗留的html5以去掉对html标签的校验
spring.thymeleaf.mode=LEGACYHTML5