SpringBoot 之 thymeleaf
thymeleaf 的maven 配置我们都知道:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
但是, 它还是很多坑的。
先看一个boot 的默认配置:
# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.cache=true # Enable template caching.
spring.thymeleaf.check-template=true # Check that the template exists before rendering it.
spring.thymeleaf.check-template-location=true # Check that the templates location exists.
spring.thymeleaf.content-type=text/html # Content-Type value.
spring.thymeleaf.enabled=true # Enable MVC Thymeleaf view resolution.
spring.thymeleaf.encoding=UTF-8 # Template encoding.
spring.thymeleaf.excluded-view-names= # Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.mode=HTML5 # Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.prefix=classpath:/templates/ # Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.suffix=.html # Suffix that gets appended to view names when building a URL.
spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain.
spring.thymeleaf.view-names= # Comma-separated list of view names that can be resolved.
注意到 spring.thymeleaf.prefix=classpath:/templates/ , 而 suffix=.html 。这两个配置是相当关键的, 不然就被坑死了!。 它的意思一定不能搞错:
spring.thymeleaf.prefix=classpath:/templates/ 指明 thymeleaf 查找view 资源的根目录。 默认是 /templates/ 目录。 templates 的意思正确理解是模板 文件目录。
spring.thymeleaf.suffix=.html 指明 thymeleaf 查找view 资源时候使用的 后缀。 默认是 html 文件。
上面两个配置, 我们一般不需要做什么修改。 有意思的是下面的 配置:
spring.thymeleaf.excluded-view-names 要排除的 view Name,也就是 试图名称。他可以是一个包含星号(*)的路径名,(不是正则表达式)。
spring.thymeleaf.view-names= 可以被解析的 view Names, 也可以是一个包含星号的路径名。只有@RequestMapping返回的view 的name 落在了这个 Comma-separated list , 那么boot 才会使用ThymeleafViewResolver 进行解析。
ThymeleafViewResolver 关键代码是:
protected boolean canHandle(String viewName, Locale locale) {
String[] viewNamesToBeProcessed = this.getViewNames();
String[] viewNamesNotToBeProcessed = this.getExcludedViewNames();
return (viewNamesToBeProcessed == null || PatternMatchUtils.simpleMatch(viewNamesToBeProcessed, viewName)) && (viewNamesNotToBeProcessed == null || !PatternMatchUtils.simpleMatch(viewNamesNotToBeProcessed, viewName));
}
ThymeleafViewResolver 的处理view 的优先级是第一位,也就是如果它能够处理,那么就交给他处理。 否则交给其他 ViewResolver处理。
我们可以通过 spring.thymeleaf.template-resolver-order 进行配置。
注意, 如果ThymeleafViewResolver 能够处理, 但是,资源却找不到,那么会看到一个后台错误,如:
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/v222/index", template might not exist or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
如果ThymeleafViewResolver 不能处理,当然, 是看不到这个错误的。 也就是说, 我们可以同时使用 ThymeleafViewResolver , 和 JspxxViewResolver, 即同时使用 thymeleaf 模板和 jsp 页面试图技术。 但是他们有个先后顺序。
关于thymeleaf 的语法, 这里不在赘述,请参见:
http://www.cnblogs.com/nuoyiamy/p/5591559.html
SpringBoot 之 thymeleaf的更多相关文章
- Springboot+JPA+Thymeleaf 校园博客完整小网站
本文所属[知识林]:http://www.zslin.com/web/article/detail/35 此项目是一个比较简易的校园博客.麻雀虽小五脏俱全,虽然是比较简易的但是涉及的知识点还是比较全面 ...
- 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础授权权限
上一篇<[原]无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限>介绍了实现Shiro的基础认证.本篇谈谈实现 ...
- 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限
开发环境搭建参见<[原]无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页> 需求: ① 除了登录页面,在地址栏直接访问其他 ...
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
- 从.Net到Java学习第九篇——SpringBoot下Thymeleaf
从.Net到Java学习系列目录 Thymeleaf概述 Thymeleaf 是一个流行的模板引擎,该模板引擎采用java语言开发.模板引擎是一个技术名称,是跨领域平台的概念,在java语言体系下有模 ...
- 从.Net到Java学习第六篇——SpringBoot+mongodb&Thymeleaf&模型验证
SpringBoot系列目录 SpringBoot整合mongodb MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.如果你没用过Mong ...
- SpringBoot 之Thymeleaf模板.
一.前言 Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1.JSP 最明显的问题在于它看起来像HTML或X ...
- IDEA上创建 Maven SpringBoot+mybatisplus+thymeleaf 项目
概述 在WEB领域,Java也是在不断的探索和改进,从开始的JSP--->Struts1--->Struts2+Spring--->Spring MVC--->SpringBo ...
- springboot整合thymeleaf+tiles示例
网上关于此框架的配置实在不多,因此想记录下来以防忘记 因为公司框架基于上述(公司采用gradle构建项目,楼主采用的是maven),所以楼主能少走些弯路: 1.创建springboot-maven项目 ...
- springboot整合Thymeleaf模板引擎
引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...
随机推荐
- java中求余%与取模floorMod的区别
初学java的时候接触的%这个符号 百分号? 求余? 取模? 我只知道不是百分号,好像是求余,听别人那叫求模运算符,跟求余一样,于是我便信了. 思考之后开始迷糊,然后经过多次考证得到以下结论. 首先, ...
- nginx 开启高效文件传输模式
(1) sendfile 参数用于开启文件的高效传输模式,该参数实际上是激活了 sendfile() 功能,sendfile() 是作用于两个文件描述符之间的数据拷贝函数,这个拷贝操作是在内核之中的, ...
- 莫烦tensorflow(6)-tensorboard
import tensorflow as tfimport numpy as np def add_layer(inputs,in_size,out_size,n_layer,activation_f ...
- django 简易版搭建
1.根目录下创建mysql.cnf文件 [client]database = identimguser = rootpassword = roothost = 127.0.0.1port = 3306 ...
- 框架tensorflow3
tensorflow3 tensorflow 可视化好帮手: tf.train.SummaryWriter报错,改为tf.summary.FileWriter 软件包安装yum install sql ...
- Linux命令学习之路——内容剪切:cut
使用者:所有角色 用法:cut [ -bcdfn ] [ --complement ] filename 作用:截取文件中的部分字段用于展示或存储到新文件中 应用场景: 1.内容展示 : 截取一个或多 ...
- Python全栈之路---数据类型—字符串
字符串:有序的字符的集合,用于存储和表示基本的文本信息,一对单.双.或三引号中间包含的内容称之为字符串 1.特性:有序,不可变(开辟新地址存储字符串,python解释器会定期清空不用了的已存储的) & ...
- java-IO流-字符流-FileReader、FileWriter、自定义小数组的拷贝、BufferedReader、BufferedWriter、readLine()和newLine()方法、LineNumberReader、使用指定的码表读写字符
###21.01_IO流(字符流FileReader) * 1.字符流是什么 * 字符流是可以直接读写字符的IO流 * 字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要 ...
- Linux 系统下使用dd命令备份还原MBR主引导记录
https://en.wikipedia.org/wiki/Master_boot_recordhttps://www.cyberciti.biz/faq/howto-copy-mbr/https:/ ...
- Java基础之枚举类型
枚举 为什么需要枚举 在Java开发过程中,有时需要定义一系列有限的数据,如:月份.星期等.<br> java1.5之前,主要通过定义一系列静态常量完成. class Genders{ p ...