Spring-boot-thymeleaf

Source: Internet
Author: User
Tags cdata closing tag maven central sonatype sonatype nexus

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

The solution to the following problem is to upgrade to THYMELEAF3

74.9 Use Thymeleaf 3

By default, spring-boot-starter-thymeleaf uses Thymeleaf 2.1. If you spring-boot-starter-parent is using the, you can use Thymeleaf 3 by overriding thymeleaf.version thymeleaf-layout-dialect.version the and 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 is managing dependencies yourself, look on for the list of artifacts that is spring-boot-dependencies related to those.

To avoid a warning message about the HTML 5 template mode being deprecated and the HTML template mode being used instead, Also want to explicitly configure spring.thymeleaf.mode HTML -to IS, for example:

Spring.thymeleaf.mode:HTML

Please refer to the Thymeleaf 3 for sample to see this in action.

If you is using any of the other auto-configured Thymeleaf Extras (Spring Security, Data Attribute, or Java 8 time) your S Hould also override each of the their versions to one that's 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  >  

Might also want to add <thymeleaf-extras-java8time.version>3.0.0.release</thymeleaf?- Extras-java8time.ve? Rsion>–samuel Eustachi June 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: The element Type "IMG" must be terminated by a matching closing tag "</img>".

1, must have the end tag, this is very annoying, do not add on the error, like the following:

Org.xml.sax.SAXParseException: The element type "input" must be terminated by a matching closing tag of "</input>".

Org.xml.sax.SAXParseException: The element Type "IMG" must be terminated by a matching closing tag "</img>".

Org.xml.sax.SAXParseException: The element type "HR" must be terminated by a matching closing tag "

Org.xml.sax.SAXParseException: The element type "BR" must be terminated by a matching closing tag "</br>".

2. The included template must also have a closed label. I want to introduce the public head and bottom, the head is OK, there is a

<div class= "container" th:fragment= "Footer" >
<div>
<p>antsclub 2013-2015</p>
</div>
<script th:src= "@{bootstrap334/js/jquery.min.js}" ></script>
<script th:src= "@{bootstrap334/js/bootstrap.min.js}" ></script>
</div>

3, the introduction of a CSS file, JS file also have to use Thymeleaf label, like the above example

If your code uses the HTML5 standard, and the thymeleaf version would have stayed at 2.x, then if not <input> closed, as follows:

The following error will be thrown.

org.xml.sax.SAXParseException: 元素类型 "input" 必须由匹配的结束标记 "</input>" 终止。
Solution 1. Follow Thymeleaf old version

If your Thymeleaf cannot be changed, then your HTML standard will only stay in the old version. You must strictly abide by the XML definition, <input> plus the end tag </input> . This is obviously unfriendly to HTML5.

2. Upgrade to Thymeleaf 3 new version

It's time to try to use Thymeleaf 3. Thymeleaf 3 uses a new parsing system.

Thymeleaf 3 is no longer based on XML structure. Due to the introduction of the new parsing engine, the template's content format no longer requires strict adherence to the XML specification. That is, do not ask for tag closure, attribute quotes, and so on. Of course, for legibility, it is recommended that you write the template according to the criteria for XML.

Thymeleaf 3 uses a new parser named Attoparser 2. A new, event-based (non-sax-compliant) parser, Attoparser developed by Thymeleaf's authors, conforms to the Thymeleaf style.

Attoparser provides Thymeleaf 32 key features:

    • XML and HTML5 (non-XML) tags are fully supported, eliminating the need for external markup balancing operations.
    • Lossless parsing so that the output is processed in a markup similar to the original template with the highest precision.

So the following format is valid in Thymeleaf 3:

<div>
Thymeleaf 3 Other aspects of the resolution improvement 1. Enable parsing of validation

The Thymeleaf 2.1 provides two VALID* template modes, named VALIDXHTML and VALIDXML , in which Thymeleaf 3 will no longer exist. The new parsing infrastructure does not provide HTML or XML validation, which means that the template markup cannot be verified to conform to the specified DTD or XML schema definition during parsing.

2. No longer required <![CDATA[ ... ]]>

Thymeleaf 2.1 requires that the contents of the <script> tag be encapsulated in CDATA so that any < or symbol used > does not interfere with XML-based parsing:

<script>/*<![CDATA[*/  var user = ...  if (user.signupYear < 1990) {    alert(‘You\‘ve been here for a long time!‘);  }/*]]>*/</script>

In Thymeleaf 3, you do not need to do this, and the code immediately becomes clean and concise:

<script>  var user = ...  if (user.signupYear < 1990) {    alert(‘You\‘ve been here for a long time!‘);  }</script>
Reference documents
    • 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 the Get starter with Thymeleaf 3 and spring MVC are by using Spring MVC 4 Quickstart Maven archetype. The archetype is updated to support Thymeleaf 3. The changes that is made to the archetype is described below.

Dependencies

The project uses Spring Platform BOM for dependencies management and it does not yet (as time of writing this post) Decla Re 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</< Span class= "Hljs-title" >version></DEPENDENCY>   

The application generated with the archetype uses Java 8 time dialect and since Thymeleaf API changed, the dialect Depende Ncy must be updated too. Before it is available on 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> << Span class= "Hljs-title" >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 does not have a change in this project. If you were migrating a real project, you could be interested in the reading Migration Guide.

References
    • Thymeleaf 3 Release Info
    • Thymeleaf 3 Migration Guide
    • Spring MVC 4 Quickstart Maven archetype
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.