SlideShare a Scribd company logo
Thymeleaf
Introduction
2013.3 anthonychen
Thymeleaf 簡介

● Java 實現的 template engine
● 以 XML / XHTML/HTML5 為基礎
● 可以在非 web 的環境下運作
● 完全取代 JSP & JSTL
Thymeleaf 的優點

● HTML 5
● 對 Web art designer 友善
● 與 Spring MVC 無縫整合
● 豐富的 ecosystem
● 擴充性高的 dialect 架構
Ecosystem
●   Thymeleaf module for Play Framework 1.2
    http://www.playframework.com/modules/thymeleaf-1.0/home
●   Thymeleaf + Tiles 2
    https://github.com/thymeleaf/thymeleaf-extras-tiles2
●   Thymeleaf + Spring Security 3
    https://github.com/thymeleaf/thymeleaf-extras-springsecurity3
●   Thymeleaf eclipse IDE plugin
    https://sourceforge.net/projects/thymeleaf/files/thymeleaf-extras-eclipse-plugin
●   Thymeleaf + Spring MVC Maven archetype
    http://www.lordofthejars.com/2012/01/once-upon-time-and-long-ago-i-heard.html
●   Thymeleaf + Conditional Comments https://github.com/thymeleaf/thymeleaf-extras-
    conditionalcomments
Dialects
● Spring Dialect (build-in)
● Pages Dialect for Thymeleaf
● Layout Dialect
● Dandelion-DataTables Dialect
● Joda Time Dialect
● Apache Shiro Dialect
開始使用 Thymeleaf
1. 建立 html檔案並加上 HTML 5 及 thymeleaf xmlns宣告
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:th="http://www.thymeleaf.org">


2. Expression 語法 - OGNL or Spring Expression Language
●    存取變數 expressions
●    Selection expressions
●    文字處理與國際化 expressions
●    URL expressions
Thymeleaf Expressions (1)
● 存取變數 expressions
<td th:text="${#session.user.name}">Name</td>

<h1 th:text="${message}">Hello, world!</h1>

<tr th:each="user: ${userList}">

<legend th:if="${#lists.isEmpty(userList)}">No data</legend>

● Selection (asterisk) expressions
<form id="userForm" name="userForm" action="#" method="POST"
th:action="@{/user/save}" th:object="${user}">

<input type="text" id="inputAccount" placeholder="Account" th:
field="*{account}"/>
Thymeleaf Expressions (2)
● 文字處理與國際化 expressions
  <table>
    ...
    <th th:text="#{header.address.city}">...</th>
    <th th:text="#{header.address.country}">...</th>
    ...
  </table>

● URL expressions
  <div shiro:authenticated="false">
      <a href="#login" th:href="@{/login(id=${userId})}">
           Login
      </a>
  </ul>
Thymeleaf Helper Objects
#dates: 處理 java.util.Date 物件。包括建立、格式化、取得年/月/日...等
#calendars: 類似 #dates,用來處理 java.util.Calendar 物件
#numbers: 格式化或轉換數字格式
#strings: 處理字串物件
#objects: 單純處理物件的工具,例如 Null Safe 判斷
#bools: 判斷物件或陣列 boolean 值的工具.
#arrays:: 陣列工具
#lists: java.utils.List 工具
#sets: java.utils.Set 工具
#maps: java.utils.Map 工具
#aggregates: 處理 Collection 或 Array 聚合運算,例如計算陣列內容加總或平均
#messages : 處理國際化語言文字
Thymeleaf Helper Object 範例
日期格式化: ${#dates.format(date, ‘dd/MMM/yyyy HH:mm’)}
建立日期物件以取得現在時間: ${#dates.createNow()}
檢查字串 empty 或 null: ${#strings.isEmpty(name)}
其他字串處理: #strings.indexOf, #strings.substring, #strings.replace, #strings.
prepend, #strings.append, #strings.toUpperCase, #strings.toLowerCase,
#strings.trim, #strings.length, #strings.abbreviate
字串集合物件處理: #strings.listJoin, #strings.arrayJoin, #strings.setJoin, strings.
listSplit, strings.arraySplit, strings.setSplit
List 物件處理: ${#lists.size(list)}, ${#lists.isEmpty(list)}, ${#lists.contains(list,
element)}
聚合運算: ${#aggregates.sum(array), ${#aggregates.avg(array)}
國際化: ${#messages.msg(‘msgKey’)}, ${#messages.msg(‘msgKey’, param1,
param2)}
Template 運用
● Apache Tiles
  ○ https://github.com/thymeleaf/thymeleaf-extras-tiles2
● Thymol - th:include
  ○ http://sourceforge.net/u/jjbenson/wiki/thymol/
● Layout Dialect
  ○ https://github.com/ultraq/thymeleaf-layout-dialect
Thymeleaf + Spring MVC 3 (1)
   在 Spring configuration file 設定 Spring Standard Dialect
<!-- Thymeleaf template recolver -->
<bean id="templateResolver"
      class="org.thymeleaf.templateresolver.ServletContextTemplateResolver" >
    <property name="cacheable" value="false"/>
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".html"/>
    <property name="templateMode" value="HTML5"/>
    <property name="characterEncoding" value="UTF-8"/>
</bean>

<!-- Thymeleaf template engine -->
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine" >
    <property name="templateResolver" ref="templateResolver" />
    <property name="dialects" >
        <set>
            <bean class="org.thymeleaf.spring3.dialect.SpringStandardDialect" />
        </set>
    </property>
</bean>
Thymeleaf + Spring MVC 3 (2)
在 Spring configuration file 設定 View resolver
<!-- Thymeleaf view resolver -->
<bean id="viewResolver" class="org.thymeleaf.spring3.view.
ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine"/>
    <property name="characterEncoding" value="UTF-8"/>
</bean>


如果你需要客製自己的 View bean
<bean name="main" class="org.thymeleaf.spring3.view.ThymeleafView" >
    <property name="staticVariables" >
        <map>
            <entry key="header" value="Hello" />
            <entry key="footer" value="Create by anthonychen" />
        </map>
    </property>
</bean>
擴充 Thymeleaf
 ● 建立 attribute 或 element processor (IProcessor Interface)

 ● 建立 dialect - 繼承 AbstractDialect

 ● 將 dialect 加入 thymeleaf 設定


SAY HELLO! EXTENDING THYMELEAF IN 5 MINUTES
HTTP://WWW.THYMELEAF.ORG/SAYHELLOEXTENDINGTHYMELEAF5MINUTES.HTML


SAY HELLO AGAIN! EXTENDING THYMELEAF EVEN MORE IN ANOTHER 5 MINUTES
HTTP://WWW.THYMELEAF.
ORG/SAYHELLOAGAINEXTENDINGTHYMELEAFEVENMORE5MINUTES.HTML
參考資源
●   Getting started
     ○   Getting started with the Standard dialects in 5 minutes
         http://www.thymeleaf.org/standarddialect5minutes.html
     ○   Standard URL Syntax
         http://www.thymeleaf.org/standardurlsyntax.html
●   Documentation http://www.thymeleaf.org/documentation.html
     ○   Using Thymeleaf
     ○   Thymeleaf + Spring 3
     ○   Extending Thymeleaf
●   User forum http://forum.thymeleaf.org/
●   Other resource
     ○   Rich HTML email in Spring with Thymeleaf (http://www.thymeleaf.org/springmail.html)
     ○   Using Thymeleaf with Spring MVC (http://blog.zenika.com/index.php?
         post/2013/01/21/using-thymeleaf-with-spring-mvc)
你還可以...
● Thymeleaf + Ajax
  JQuery, Dojo, Spring JavaScript (part of Spring WebFlow)
● Thymeleaf + Responsive CSS Framework
  Bootstraps, Foundation, Skeleton
● Thymeleaf + JRebel
● 寫個 dialect 吧
   ○ JSP taglib --> Thymeleaf dialect
   ○ JQuery plugin --> Thymeleaf dialect
Thank You!

More Related Content

PDF
Introduction to thymeleaf
PDF
Introducing thymeleaf
PDF
Broadleaf Presents Thymeleaf
PDF
Extending the WordPress REST API - Josh Pollock
PDF
Introduzione JQuery
PPT
Php mysql
PDF
Javascript
PPTX
Phphacku iitd
Introduction to thymeleaf
Introducing thymeleaf
Broadleaf Presents Thymeleaf
Extending the WordPress REST API - Josh Pollock
Introduzione JQuery
Php mysql
Javascript
Phphacku iitd

What's hot (20)

PPTX
PHP for hacks
PDF
Object Oriented PHP - PART-2
PDF
Php converted pdf
PDF
Current state-of-php
KEY
Using PHP
PPT
Introduction To Lamp
PDF
Being a jsp
PPT
symfony & jQuery (phpDay)
PDF
Java script tutorial
PDF
Ch. 8 script free pages
PDF
Workshop 8: Templating: Handlebars, DustJS
PDF
Laravel the right way
PDF
Laravel, the right way - PHPConference 2016
PDF
Ch. 9 jsp standard tag library
PPTX
Node.js System: The Approach
PPTX
Cake PHP 3 Presentaion
PDF
Ch. 7 beeing a jsp
PHP for hacks
Object Oriented PHP - PART-2
Php converted pdf
Current state-of-php
Using PHP
Introduction To Lamp
Being a jsp
symfony & jQuery (phpDay)
Java script tutorial
Ch. 8 script free pages
Workshop 8: Templating: Handlebars, DustJS
Laravel the right way
Laravel, the right way - PHPConference 2016
Ch. 9 jsp standard tag library
Node.js System: The Approach
Cake PHP 3 Presentaion
Ch. 7 beeing a jsp
Ad

Viewers also liked (20)

PDF
Spring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
PDF
Shootout! template engines on the jvm
PDF
Thymeleaf, will it blend?
PDF
Spring Framework - MVC
PDF
Shootout! Template engines for the JVM
PPTX
Website design configuration
PPTX
App Design + Layout
PDF
Thymeleafのすすめ
PPTX
Spring User Group Thymeleaf 08-21-2013
PPT
DSpace UI Prototype Challenge: Spring Boot + Thymeleaf
PDF
Java™ in Web 2.0
PDF
Modern Java web applications with Spring Boot and Thymeleaf
PPTX
Introduction to jQuery
PPTX
Introduction to spring boot
PDF
Spring Boot
PPTX
Introduction to JPA Framework
PDF
Introduction to Spring Boot!
PDF
OAuth2 and Spring Security
PDF
REST with Spring Boot #jqfk
PPTX
Spring Boot Tutorial
Spring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
Shootout! template engines on the jvm
Thymeleaf, will it blend?
Spring Framework - MVC
Shootout! Template engines for the JVM
Website design configuration
App Design + Layout
Thymeleafのすすめ
Spring User Group Thymeleaf 08-21-2013
DSpace UI Prototype Challenge: Spring Boot + Thymeleaf
Java™ in Web 2.0
Modern Java web applications with Spring Boot and Thymeleaf
Introduction to jQuery
Introduction to spring boot
Spring Boot
Introduction to JPA Framework
Introduction to Spring Boot!
OAuth2 and Spring Security
REST with Spring Boot #jqfk
Spring Boot Tutorial
Ad

Similar to Thymeleaf Introduction (20)

PDF
19servlets
PDF
Servlets Java Slides & Presentation
PDF
Servlets
PDF
The JavaFX Ecosystem
PDF
Twig, the flexible, fast, and secure template language for PHP
PPTX
Use Symfony2 components inside WordPress
PDF
vJUG - The JavaFX Ecosystem
PDF
Core data intermediate Workshop at NSSpain 2013
PDF
Performance Tuning Your Puppet Infrastructure - PuppetConf 2014
PDF
Twig: Friendly Curly Braces Invade Your Templates!
PDF
.NET @ apache.org
PDF
Lightweight web frameworks
PDF
The JavaFX Ecosystem
PDF
Mojolicious
PDF
The JavaFX Ecosystem
PDF
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
KEY
Rapid Prototyping FTW!!!
PDF
Play Framework
PDF
Puppet at Bazaarvoice
PDF
Apache Zeppelin and Helium @ApacheCon 2017 may, FL
19servlets
Servlets Java Slides & Presentation
Servlets
The JavaFX Ecosystem
Twig, the flexible, fast, and secure template language for PHP
Use Symfony2 components inside WordPress
vJUG - The JavaFX Ecosystem
Core data intermediate Workshop at NSSpain 2013
Performance Tuning Your Puppet Infrastructure - PuppetConf 2014
Twig: Friendly Curly Braces Invade Your Templates!
.NET @ apache.org
Lightweight web frameworks
The JavaFX Ecosystem
Mojolicious
The JavaFX Ecosystem
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Rapid Prototyping FTW!!!
Play Framework
Puppet at Bazaarvoice
Apache Zeppelin and Helium @ApacheCon 2017 may, FL

Recently uploaded (20)

PPTX
TLE Review Electricity (Electricity).pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Approach and Philosophy of On baking technology
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Tartificialntelligence_presentation.pptx
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PPTX
1. Introduction to Computer Programming.pptx
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
A comparative study of natural language inference in Swahili using monolingua...
PPTX
A Presentation on Artificial Intelligence
PPTX
A Presentation on Touch Screen Technology
PDF
Encapsulation theory and applications.pdf
PDF
August Patch Tuesday
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
project resource management chapter-09.pdf
TLE Review Electricity (Electricity).pptx
Assigned Numbers - 2025 - Bluetooth® Document
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Approach and Philosophy of On baking technology
A comparative analysis of optical character recognition models for extracting...
Accuracy of neural networks in brain wave diagnosis of schizophrenia
1 - Historical Antecedents, Social Consideration.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Tartificialntelligence_presentation.pptx
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
1. Introduction to Computer Programming.pptx
WOOl fibre morphology and structure.pdf for textiles
Agricultural_Statistics_at_a_Glance_2022_0.pdf
A comparative study of natural language inference in Swahili using monolingua...
A Presentation on Artificial Intelligence
A Presentation on Touch Screen Technology
Encapsulation theory and applications.pdf
August Patch Tuesday
Hindi spoken digit analysis for native and non-native speakers
project resource management chapter-09.pdf

Thymeleaf Introduction

  • 2. Thymeleaf 簡介 ● Java 實現的 template engine ● 以 XML / XHTML/HTML5 為基礎 ● 可以在非 web 的環境下運作 ● 完全取代 JSP & JSTL
  • 3. Thymeleaf 的優點 ● HTML 5 ● 對 Web art designer 友善 ● 與 Spring MVC 無縫整合 ● 豐富的 ecosystem ● 擴充性高的 dialect 架構
  • 4. Ecosystem ● Thymeleaf module for Play Framework 1.2 http://www.playframework.com/modules/thymeleaf-1.0/home ● Thymeleaf + Tiles 2 https://github.com/thymeleaf/thymeleaf-extras-tiles2 ● Thymeleaf + Spring Security 3 https://github.com/thymeleaf/thymeleaf-extras-springsecurity3 ● Thymeleaf eclipse IDE plugin https://sourceforge.net/projects/thymeleaf/files/thymeleaf-extras-eclipse-plugin ● Thymeleaf + Spring MVC Maven archetype http://www.lordofthejars.com/2012/01/once-upon-time-and-long-ago-i-heard.html ● Thymeleaf + Conditional Comments https://github.com/thymeleaf/thymeleaf-extras- conditionalcomments
  • 5. Dialects ● Spring Dialect (build-in) ● Pages Dialect for Thymeleaf ● Layout Dialect ● Dandelion-DataTables Dialect ● Joda Time Dialect ● Apache Shiro Dialect
  • 6. 開始使用 Thymeleaf 1. 建立 html檔案並加上 HTML 5 及 thymeleaf xmlns宣告 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> 2. Expression 語法 - OGNL or Spring Expression Language ● 存取變數 expressions ● Selection expressions ● 文字處理與國際化 expressions ● URL expressions
  • 7. Thymeleaf Expressions (1) ● 存取變數 expressions <td th:text="${#session.user.name}">Name</td> <h1 th:text="${message}">Hello, world!</h1> <tr th:each="user: ${userList}"> <legend th:if="${#lists.isEmpty(userList)}">No data</legend> ● Selection (asterisk) expressions <form id="userForm" name="userForm" action="#" method="POST" th:action="@{/user/save}" th:object="${user}"> <input type="text" id="inputAccount" placeholder="Account" th: field="*{account}"/>
  • 8. Thymeleaf Expressions (2) ● 文字處理與國際化 expressions <table> ... <th th:text="#{header.address.city}">...</th> <th th:text="#{header.address.country}">...</th> ... </table> ● URL expressions <div shiro:authenticated="false"> <a href="#login" th:href="@{/login(id=${userId})}"> Login </a> </ul>
  • 9. Thymeleaf Helper Objects #dates: 處理 java.util.Date 物件。包括建立、格式化、取得年/月/日...等 #calendars: 類似 #dates,用來處理 java.util.Calendar 物件 #numbers: 格式化或轉換數字格式 #strings: 處理字串物件 #objects: 單純處理物件的工具,例如 Null Safe 判斷 #bools: 判斷物件或陣列 boolean 值的工具. #arrays:: 陣列工具 #lists: java.utils.List 工具 #sets: java.utils.Set 工具 #maps: java.utils.Map 工具 #aggregates: 處理 Collection 或 Array 聚合運算,例如計算陣列內容加總或平均 #messages : 處理國際化語言文字
  • 10. Thymeleaf Helper Object 範例 日期格式化: ${#dates.format(date, ‘dd/MMM/yyyy HH:mm’)} 建立日期物件以取得現在時間: ${#dates.createNow()} 檢查字串 empty 或 null: ${#strings.isEmpty(name)} 其他字串處理: #strings.indexOf, #strings.substring, #strings.replace, #strings. prepend, #strings.append, #strings.toUpperCase, #strings.toLowerCase, #strings.trim, #strings.length, #strings.abbreviate 字串集合物件處理: #strings.listJoin, #strings.arrayJoin, #strings.setJoin, strings. listSplit, strings.arraySplit, strings.setSplit List 物件處理: ${#lists.size(list)}, ${#lists.isEmpty(list)}, ${#lists.contains(list, element)} 聚合運算: ${#aggregates.sum(array), ${#aggregates.avg(array)} 國際化: ${#messages.msg(‘msgKey’)}, ${#messages.msg(‘msgKey’, param1, param2)}
  • 11. Template 運用 ● Apache Tiles ○ https://github.com/thymeleaf/thymeleaf-extras-tiles2 ● Thymol - th:include ○ http://sourceforge.net/u/jjbenson/wiki/thymol/ ● Layout Dialect ○ https://github.com/ultraq/thymeleaf-layout-dialect
  • 12. Thymeleaf + Spring MVC 3 (1) 在 Spring configuration file 設定 Spring Standard Dialect <!-- Thymeleaf template recolver --> <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver" > <property name="cacheable" value="false"/> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8"/> </bean> <!-- Thymeleaf template engine --> <bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine" > <property name="templateResolver" ref="templateResolver" /> <property name="dialects" > <set> <bean class="org.thymeleaf.spring3.dialect.SpringStandardDialect" /> </set> </property> </bean>
  • 13. Thymeleaf + Spring MVC 3 (2) 在 Spring configuration file 設定 View resolver <!-- Thymeleaf view resolver --> <bean id="viewResolver" class="org.thymeleaf.spring3.view. ThymeleafViewResolver"> <property name="templateEngine" ref="templateEngine"/> <property name="characterEncoding" value="UTF-8"/> </bean> 如果你需要客製自己的 View bean <bean name="main" class="org.thymeleaf.spring3.view.ThymeleafView" > <property name="staticVariables" > <map> <entry key="header" value="Hello" /> <entry key="footer" value="Create by anthonychen" /> </map> </property> </bean>
  • 14. 擴充 Thymeleaf ● 建立 attribute 或 element processor (IProcessor Interface) ● 建立 dialect - 繼承 AbstractDialect ● 將 dialect 加入 thymeleaf 設定 SAY HELLO! EXTENDING THYMELEAF IN 5 MINUTES HTTP://WWW.THYMELEAF.ORG/SAYHELLOEXTENDINGTHYMELEAF5MINUTES.HTML SAY HELLO AGAIN! EXTENDING THYMELEAF EVEN MORE IN ANOTHER 5 MINUTES HTTP://WWW.THYMELEAF. ORG/SAYHELLOAGAINEXTENDINGTHYMELEAFEVENMORE5MINUTES.HTML
  • 15. 參考資源 ● Getting started ○ Getting started with the Standard dialects in 5 minutes http://www.thymeleaf.org/standarddialect5minutes.html ○ Standard URL Syntax http://www.thymeleaf.org/standardurlsyntax.html ● Documentation http://www.thymeleaf.org/documentation.html ○ Using Thymeleaf ○ Thymeleaf + Spring 3 ○ Extending Thymeleaf ● User forum http://forum.thymeleaf.org/ ● Other resource ○ Rich HTML email in Spring with Thymeleaf (http://www.thymeleaf.org/springmail.html) ○ Using Thymeleaf with Spring MVC (http://blog.zenika.com/index.php? post/2013/01/21/using-thymeleaf-with-spring-mvc)
  • 16. 你還可以... ● Thymeleaf + Ajax JQuery, Dojo, Spring JavaScript (part of Spring WebFlow) ● Thymeleaf + Responsive CSS Framework Bootstraps, Foundation, Skeleton ● Thymeleaf + JRebel ● 寫個 dialect 吧 ○ JSP taglib --> Thymeleaf dialect ○ JQuery plugin --> Thymeleaf dialect