首页 > 代码库 > spring-boot-thymeleaf
spring-boot-thymeleaf
https://github.com/kolorobot/spring-boot-thymeleaf
http://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/#howto-use-thymeleaf-3
https://github.com/spring-projects/spring-boot/blob/v1.4.1.RELEASE/spring-boot-samples/spring-boot-sample-web-thymeleaf3/src/main/resources/application.properties
http://blog.imyxiao.com/2016/11/26/Spring-Boot-thymeleaf-3/
https://github.com/kolorobot/spring-boot-thymeleaf
解决下面的问题的办法是升级到Thymeleaf3
74.9 Use Thymeleaf 3
By default, spring-boot-starter-thymeleaf
uses Thymeleaf 2.1. If you are using the spring-boot-starter-parent
, you can use Thymeleaf 3 by overriding thethymeleaf.version
and thymeleaf-layout-dialect.version
properties, for example:
<properties> <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version></properties>
if you are managing dependencies yourself, look at |
To avoid a warning message about the HTML 5 template mode being deprecated and the HTML template mode being used instead, you may also want to explicitly configure spring.thymeleaf.mode
to be HTML
, for example:
spring.thymeleaf.mode: HTML
Please refer to the Thymeleaf 3 sample to see this in action.
If you are using any of the other auto-configured Thymeleaf Extras (Spring Security, Data Attribute, or Java 8 Time) you should also override each of their versions to one that is compatible with Thymeleaf 3.0.
http://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/#howto-use-thymeleaf-3
It‘s much simpler, just read this: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-use-thymeleaf-3
<properties><thymeleaf.version>3.0.2.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version></properties>
You might also want to add <thymeleaf-extras-java8time.version>3.0.0.RELEASE</thymeleaf?-extras-java8time.ve?rsion> – Samuel EUSTACHI Jun 6 at 9:42
https://stackoverflow.com/questions/37439369/spring-boot-and-thymeleaf-3-0-0-release-integration
http://blog.imyxiao.com/2016/11/26/Spring-Boot-thymeleaf-3/
http://www.thymeleaf.org/doc/articles/thymeleaf3migration.html
https://github.com/spring-projects/spring-boot/tree/v1.4.1.RELEASE/spring-boot-samples/spring-boot-sample-web-thymeleaf3
org.xml.sax.SAXParseException: 元素类型 "img" 必须由匹配的结束标记 "</img>" 终止。
1、必须要有结束标签,这个很烦,不加就报错,像下面这样:
org.xml.sax.SAXParseException: 元素类型 "input" 必须由匹配的结束标记 "</input>" 终止。
org.xml.sax.SAXParseException: 元素类型 "img" 必须由匹配的结束标记 "</img>" 终止。
org.xml.sax.SAXParseException: 元素类型 "hr" 必须由匹配的结束标记 "</hr>" 终止。
org.xml.sax.SAXParseException: 元素类型 "br" 必须由匹配的结束标记 "</br>" 终止。
2、包含的模板,也必须有闭合标签。我要引入公共的头部和底部,头部还好说,外面有个<head>包着,底部必须得在外面套个div,引入js的代码也得包括在div里,不爽啊,像这样:
<div class="container" th:fragment="footer">
<hr></hr>
<div>
<p>AntsClub 2013-2015</p>
</div>
<script th:src="http://www.mamicode.com/@{bootstrap334/js/jquery.min.js}"></script>
<script th:src="http://www.mamicode.com/@{bootstrap334/js/bootstrap.min.js}"></script>
</div>
3、引入个css文件,js文件也得用thymeleaf的标签,就像上面的例子
如果你的代码使用了 HTML5 的标准,而Thymeleaf 版本来停留在 2.x ,那么如果没有把<input>
闭合,如下:
<form> First name:<br><input type="text" name="firstname"><br> Last name:<br><input type="text" name="lastname"></form>
就会抛出如下错误。
org.xml.sax.SAXParseException: 元素类型 "input" 必须由匹配的结束标记 "</input>" 终止。
解决方案
1. 沿用 Thymeleaf 老版本
如果你的 Thymeleaf 不能变更,那么你的 HTML 标准也只能停留在老版本了。你必须严格遵守 XML 定义,在<input>
加上结束标记</input>
。这显然,对于 HTML5 不友好。
2. 升级至 Thymeleaf 3 新版本
是时候尝试下使用 Thymeleaf 3 了。Thymeleaf 3 使用了新的解析系统。
Thymeleaf 3 不再是基于XML结构的。由于引入新的解析引擎,模板的内容格式不再需要严格遵守XML规范。即不在要求标签闭合,属性加引号等等。当然,出于易读性考虑,还是推荐你按找XML的标准去编写模板。
Thymeleaf 3 使用一个名为 AttoParser 2的新解析器。 一个新的、基于事件(不符合SAX标准)的解析器,AttoParser由 Thymeleaf 的作者开发,符合 Thymeleaf 的风格。
AttoParser 提供 Thymeleaf 3 两个重要功能:
- 完全支持XML和HTML5(非XML化)标记,从而不再需要外部标记平衡操作。
- 无损解析,以便在处理的输出的标记类似于具有最高精度的原始模板。
所以下面的格式在 Thymeleaf 3 里面是合法的:
<div><img alt=logo th:src=http://www.mamicode.com/‘@{/images/logo.png}‘>>
Thymeleaf 3 其他方面的解析改进
1. 启用验证的解析
在 Thymeleaf 2.1提供了两种VALID*
模板模式,名为VALIDXHTML
和VALIDXML
,在而 Thymeleaf 3 中将不再存在。 新的解析基础结构不提供HTML或XML验证,即在解析期间无法验证模板标记是否符合指定的DTD或XML模式定义。
2. 不再需要<![CDATA[ ... ]]>
Thymeleaf 2.1 要求将<script>
标记的内容封装在 CDATA 中,以便所使用的任何<
或>
符号不会干扰基于XML的解析:
<script>/*<![CDATA[*/ var user = ... if (user.signupYear < 1990) { alert(‘You\‘ve been here for a long time!‘); }/*]]>*/</script>
而在 Thymeleaf 3 中则不需要这样做,代码立马变得简洁干净:
<script> var user = ... if (user.signupYear < 1990) { alert(‘You\‘ve been here for a long time!‘); }</script>
参考文献
- https://github.com/thymeleaf/thymeleaf/issues/390
https://waylau.com/thymeleaf-3-adopts-a-new-parsing-system/
THYMELEAF 3 - GET STARTED QUICKLY WITH THYMELEAF 3 AND SPRING MVC
Thymeleaf 3 release arrived. The new version brings plenty of new features like HTML5 support as well as Text templates support with no markup - [# th:utext="${thymeleaf.version}" /]
, improved inline capabilities - <p>Thymeleaf [[${thymeleaf.version}]] is great!</p>
, performence improvements and much more.
The easiest way the get starter with Thymeleaf 3 and Spring MVC is by using Spring MVC 4 Quickstart Maven Archetype. The archetype was updated to support Thymeleaf 3. The changes that are made to the archetype are described below.
Dependencies
The project uses Spring Platform BOM for dependencies management, but it does not yet (as time of writing this post) declare dependency on Thymeleaf 3, so I needed to declare the versions manually.
- Thymeleaf:
<dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.0.RELEASE</version></dependency>
- Thymeleaf Spring 4:
<dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>3.0.0.RELEASE</version></dependency>
- Thymeleaf Spring Security 4:
<dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity4</artifactId> <version>3.0.0.RELEASE</version></dependency>
The application generated with the archetype uses Java 8 Time Dialect and since Thymeleaf API changed, the dialect dependency must be updated too. Before it is available in Maven Central, we must add snapshot repository to POM:
<repository> <id>sonatype-nexus-snapshots</id> <name>Sonatype Nexus Snapshots</name> <url>https://oss.sonatype.org/content/repositories/snapshots</url> <snapshots> <enabled>true</enabled> </snapshots></repository>
And then declare the dependency:
<dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-java8time</artifactId> <version>3.0.0-SNAPSHOT</version></dependency>
Configuration changes
- Template resolver
Template resolver before:
@Beanpublic TemplateResolver templateResolver() { TemplateResolver resolver = new ServletContextTemplateResolver(); resolver.setPrefix(VIEWS); resolver.setSuffix(".html"); resolver.setTemplateMode("HTML5"); resolver.setCacheable(false); return resolver;}
Template resolver after:
@Beanpublic ITemplateResolver templateResolver() { SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver(); resolver.setPrefix(VIEWS); resolver.setSuffix(".html"); resolver.setTemplateMode(TemplateMode.HTML); resolver.setCacheable(false); return resolver;}
- Template Engine
@Beanpublic SpringTemplateEngine templateEngine() { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver()); templateEngine.addDialect(new SpringSecurityDialect()); templateEngine.addDialect(new Java8TimeDialect()); return templateEngine;}
- View Resolver:
@Beanpublic ViewResolver viewResolver() { ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver(); thymeleafViewResolver.setTemplateEngine(templateEngine()); thymeleafViewResolver.setCharacterEncoding("UTF-8"); return thymeleafViewResolver;}
Templates
The templates did not change in this project. But if you are migrating a real project, you may be interested in reading migration guide.
References
- Thymeleaf 3 release info
- Thymeleaf 3 Migration guide
- Spring MVC 4 Quickstart Maven Archetype
You may be also interested in
- Spring Boot and Thymeleaf with Maven
- Spring MVC and Thymeleaf: how to acess data from templates
- Thymeleaf Page Layouts
http://blog.codeleak.pl/2016/05/thymeleaf-3-get-started-quickly-with.html
spring-boot-thymeleaf