1. 程式人生 > >spring-boot-thymeleaf

spring-boot-thymeleaf

高精度 put ive 引入 function rri chang 解決方案 osi

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 spring-boot-dependencies

for the list of artifacts that are related to those two versions.

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="@{bootstrap334/js/jquery.min.js}"></script>
<script th:src="@{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=‘@{/images/logo.png}‘>

Thymeleaf 3 其他方面的解析改進

1. 啟用驗證的解析

在 Thymeleaf 2.1提供了兩種VALID*模板模式,名為VALIDXHTMLVALIDXML,在而 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:

@Bean
public TemplateResolver templateResolver() {
   TemplateResolver resolver = new ServletContextTemplateResolver();
   resolver.setPrefix(VIEWS);
   resolver.setSuffix(".html");
   resolver.setTemplateMode("HTML5");
   resolver.setCacheable(false);
   return resolver;
}

Template resolver after:

@Bean
public ITemplateResolver templateResolver() {
    SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
    resolver.setPrefix(VIEWS);
    resolver.setSuffix(".html");
    resolver.setTemplateMode(TemplateMode.HTML);
    resolver.setCacheable(false);
    return resolver;
}
  • Template Engine
@Bean
public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver());
    templateEngine.addDialect(new SpringSecurityDialect());
    templateEngine.addDialect(new Java8TimeDialect());
    return templateEngine;
}
  • View Resolver:
@Bean
public 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