Spring Boot—— Thymeleaf (gradle) 的简单使用
2017-12-22 12:08
731 查看
最近项目用到了Spring Boot ,但是在控制器返回html视图并渲染参数的时候,存在了疑问。后面考虑用Thymeleaf ,感觉真的不错~,下面分享给大家
总共四步:jar 引入 、控制器参数传递 、 html标签引入 、 Thymeleaf 缓存设置
一、相关Jar的引用
1、maven的引用方式:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、我现在的项目是用的Gradle,在build.gradle 里面的dependency加入以下配置:
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
二、Spring Boot 控制器Controller的配置,需要使用Model来进行参数传递(或者自定义Map)br/>@RequestMapping("/index")
model.addAttribute("loginName", "admin");
model.addAttribute("loginId", "27");
return "index";
}
因为Thymeleaf 默认的视图返回路径是 /src/java/resources/templates ,而且默认后缀是.html , 所以我们的页面视图就放到templates目录下
三、页面视图的配置
1、在html标签上引入 Thymeleaf 的标签库,然后参数输出就可以直接使用${} 了
<html xmlns="[url=http://www.w3.org/1999/xhtml]http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
例如:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
<title>im test page</title>
</head>
<body>
loginId:<span th:text="${loginId}"></span>
loginName:<span th:text="${loginName}"></span>
</body>
</html>
最后的输出结果:
四、Thymeleaf 的缓存配置
每次更改页面,如果不配置Thymeleaf 缓存设置为false,那么每次更改html页面都需要重启页面才刷新,这肯定是我们不愿意的
那么有一个简单的办法,在我们的的基础配置文件里面加入一句配置。 例如我的是application.properties 里面加入一句:
#Thymeleaf cache set
spring.thymeleaf.cache=false
重启,配置完成
总共四步:jar 引入 、控制器参数传递 、 html标签引入 、 Thymeleaf 缓存设置
一、相关Jar的引用
1、maven的引用方式:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、我现在的项目是用的Gradle,在build.gradle 里面的dependency加入以下配置:
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
二、Spring Boot 控制器Controller的配置,需要使用Model来进行参数传递(或者自定义Map)br/>@RequestMapping("/index")
model.addAttribute("loginName", "admin");
model.addAttribute("loginId", "27");
return "index";
}
因为Thymeleaf 默认的视图返回路径是 /src/java/resources/templates ,而且默认后缀是.html , 所以我们的页面视图就放到templates目录下
三、页面视图的配置
1、在html标签上引入 Thymeleaf 的标签库,然后参数输出就可以直接使用${} 了
<html xmlns="[url=http://www.w3.org/1999/xhtml]http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
例如:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
<title>im test page</title>
</head>
<body>
loginId:<span th:text="${loginId}"></span>
loginName:<span th:text="${loginName}"></span>
</body>
</html>
最后的输出结果:
四、Thymeleaf 的缓存配置
每次更改页面,如果不配置Thymeleaf 缓存设置为false,那么每次更改html页面都需要重启页面才刷新,这肯定是我们不愿意的
那么有一个简单的办法,在我们的的基础配置文件里面加入一句配置。 例如我的是application.properties 里面加入一句:
#Thymeleaf cache set
spring.thymeleaf.cache=false
重启,配置完成
相关文章推荐
- Spring Boot—— Thymeleaf (gradle) 的简单使用
- SpringBoot使用Thymeleaf3.0自定义标签,简单实例。快速学习!
- 《Springboot极简教程》使用Spring Boot, JPA, Mysql, ThymeLeaf,gradle, Kotlin快速构建一个CRUD Web App
- Springboot + gradle + websocket的简单使用
- 使用Gradle创建一个最简单的Spring Boot项目
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 68. 使用thymeleaf报异常:Not Found, status=404【从零开始学Spring Boot】
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 使用spring boot和thymeleaf提供web内容
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板