深入学习spring-boot系列(三)--使用thymeleaf模板

本文以上一篇文章“[url=http://jisonami.iteye.com/blog/2298372]深入学习spring-boot系列(二)--使用spring-data-jpa[/url]
”为原型,将里面的所有JSP页面更改成thymeleaf模板。

本文记录一下几点:
一、资源文件的约定目录结构
二、Maven配置
三、开发时修改thymeleaf模板自动重新加载配置
四、thymeleaf常用基础知识点

一、资源文件的约定目录结构
在“[url=http://jisonami.iteye.com/blog/2298372]深入学习spring-boot系列(二)--使用spring-data-jpa[/url]“一文中,只介绍了spring-boot项目源码目录结构的约定,现在继续介绍资源文件的约定目录结构
Maven的资源文件目录:/src/java/resources
spring-boot项目静态文件目录:/src/java/resources/static
spring-boot项目模板文件目录:/src/java/resources/templates
spring-boot静态首页的支持,即index.html放在以下目录结构会直接映射到应用的根目录下:
classpath:/META-INF/resources/index.html
classpath:/resources/index.html
classpath:/static/index.html
calsspath:/public/index.html

而上一篇文章“[url=http://jisonami.iteye.com/blog/2298372]深入学习spring-boot系列(二)--使用spring-data-jpa[/url]“中使用的标准maven目录/src/java/webapp目录没有了,将之前的jsp页面全部使用thymeleaf模板重新实现,并将它们放置在/src/java/resources/templates目录下。
而由于使用thymeleaf的html5模板,所以我将index.html模板文件直接放到了/src/java/resources/templates目录下。然而这个目录并不是首页文件的默认目录,所以我们需要手动将应用根路径映射到/src/java/resources/templates/index.html下。这个在spring-mvc的Controller下映射一下就可以了。
@RequestMapping("/")
public String index(){
return "index";
}

在spring-boot下,默认约定了Controller试图跳转中thymeleaf模板文件的的前缀prefix是”classpath:/templates/”,后缀suffix是”.html”
这个在application.properties配置文件中是可以修改的。
如下配置可以修改试图跳转的前缀和后缀
spring.thymeleaf.prefix: /templates/
spring.thymeleaf.suffix: .html

更过有关thymeleaf中的默认这是可以查看org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties这个类的属性
看看当前的目录结构如下:

[img]http://dl2.iteye.com/upload/attachment/0117/7183/9bf68bab-904a-3e5b-8171-b6f691c5502f.png[/img]

二、Maven配置
在pom.xml中加入如下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

原来关于spring-boot-starter-web等的依赖就可以去掉了,因为spring-boot-starter-thymeleaf是包含这些依赖的。而关于jsp的依赖也可以去掉了,因为我们已经完全抛弃jsp了。

三、开发时修改thymeleaf模板自动重新加载配置
Spring-boot使用thymeleaf时默认是有缓存的,即你把一个页面代码改了不会刷新页面的效果,你必须重新运行spring-boot的main()方法才能看到页面更改的效果。我们可以把thymeleaf的缓存关掉,用于支持页面修改后重新发布到spring-boot内嵌的tomcat中去。在application.properties配置文件中加入以下配置。
# Allow Thymeleaf templates to be reloaded at dev time
spring.thymeleaf.cache: false
server.tomcat.access_log_enabled: true
server.tomcat.basedir: target/tomcat


四、thymeleaf常用基础知识点

1、在html页面中引入thymeleaf命名空间,即<html xmlns:th=http://www.thymeleaf.org></html>,此时在html模板文件中动态的属性使用th:命名空间修饰

2、引用静态资源文件,比如CSS和JS文件,语法格式为“@{}”,如@{/js/blog/blog.js}会引入/static目录下的/js/blog/blog.js文件

3、访问spring-mvc中model的属性,语法格式为“${}”,如${user.id}可以获取model里的user对象的id属性

4、循环,在html的标签中,加入th:each=“value:${list}”形式的属性,如<span th:each=”user:${users}”></span>可以迭代users的数据

5、判断,在html标签中,加入th:if=”表达式”可以根据条件显示html元素
<span th:if="${not #lists.isEmpty(blog.publishTime)}">
<span id="publishtime" th:text="${#dates.format(blog.publishTime, 'yyyy-MM-dd HH:mm:ss')}"></span>
</span>
以上代码表示若blog.publishTime时间不为空,则显示时间

6、时间的格式化,
${#dates.format(blog.publishTime,'yyyy-MM-dd HH:mm:ss')}
表示将时间格式化为”yyyy-MM-dd HH:mm:ss”格式化写法与Java格式化Date的写法是一致的。

7、字符串拼接,有两种形式
比如拼接这样一个URL:/blog/delete/{blogId}
第一种:th:href="'/blog/delete/' + ${blog.id }"
第二种:th:href="${'/blog/delete/' + blog.id }"

更详细的代码请参考我的Github项目Jisonami的0.1.2:
[url]https://github.com/jisonami/Jisonami2/tree/0.1.2[/url]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值