SlideShare una empresa de Scribd logo
1 de 17
Descargar para leer sin conexión
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!

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

PHP for hacks
PHP for hacksPHP for hacks
PHP for hacks
 
Object Oriented PHP - PART-2
Object Oriented PHP - PART-2Object Oriented PHP - PART-2
Object Oriented PHP - PART-2
 
Php converted pdf
Php converted pdfPhp converted pdf
Php converted pdf
 
Ppt php
Ppt phpPpt php
Ppt php
 
Pourquoi WordPress n’est pas un CMS
Pourquoi WordPress n’est pas un CMSPourquoi WordPress n’est pas un CMS
Pourquoi WordPress n’est pas un CMS
 
Php
PhpPhp
Php
 
Current state-of-php
Current state-of-phpCurrent state-of-php
Current state-of-php
 
Using PHP
Using PHPUsing PHP
Using PHP
 
Introduction To Lamp
Introduction To LampIntroduction To Lamp
Introduction To Lamp
 
Being a jsp
Being a jsp     Being a jsp
Being a jsp
 
symfony & jQuery (phpDay)
symfony & jQuery (phpDay)symfony & jQuery (phpDay)
symfony & jQuery (phpDay)
 
Php
PhpPhp
Php
 
Java script tutorial
Java script tutorialJava script tutorial
Java script tutorial
 
Ch. 8 script free pages
Ch. 8 script free pagesCh. 8 script free pages
Ch. 8 script free pages
 
Workshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJSWorkshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJS
 
Laravel the right way
Laravel   the right wayLaravel   the right way
Laravel the right way
 
Laravel, the right way - PHPConference 2016
Laravel, the right way - PHPConference 2016Laravel, the right way - PHPConference 2016
Laravel, the right way - PHPConference 2016
 
Ch. 9 jsp standard tag library
Ch. 9 jsp standard tag libraryCh. 9 jsp standard tag library
Ch. 9 jsp standard tag library
 
Node.js System: The Approach
Node.js System: The ApproachNode.js System: The Approach
Node.js System: The Approach
 
Cake PHP 3 Presentaion
Cake PHP 3 PresentaionCake PHP 3 Presentaion
Cake PHP 3 Presentaion
 

Destacado

Spring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
Spring I/O 2012: Natural Templating in Spring MVC with ThymeleafSpring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
Spring I/O 2012: Natural Templating in Spring MVC with ThymeleafThymeleaf
 
Shootout! template engines on the jvm
Shootout! template engines on the jvmShootout! template engines on the jvm
Shootout! template engines on the jvmNLJUG
 
Spring Framework - MVC
Spring Framework - MVCSpring Framework - MVC
Spring Framework - MVCDzmitry Naskou
 
Shootout! Template engines for the JVM
Shootout! Template engines for the JVMShootout! Template engines for the JVM
Shootout! Template engines for the JVMJeroen Reijn
 
Website design configuration
Website design configurationWebsite design configuration
Website design configurationPuttiApps
 
App Design + Layout
App Design + LayoutApp Design + Layout
App Design + LayoutPuttiApps
 
Thymeleafのすすめ
ThymeleafのすすめThymeleafのすすめ
Thymeleafのすすめeiryu
 
Spring User Group Thymeleaf 08-21-2013
Spring User Group Thymeleaf 08-21-2013Spring User Group Thymeleaf 08-21-2013
Spring User Group Thymeleaf 08-21-2013Justin Munn
 
DSpace UI Prototype Challenge: Spring Boot + Thymeleaf
DSpace UI Prototype Challenge: Spring Boot + ThymeleafDSpace UI Prototype Challenge: Spring Boot + Thymeleaf
DSpace UI Prototype Challenge: Spring Boot + ThymeleafTim Donohue
 
Modern Java web applications with Spring Boot and Thymeleaf
Modern Java web applications with Spring Boot and ThymeleafModern Java web applications with Spring Boot and Thymeleaf
Modern Java web applications with Spring Boot and ThymeleafLAY Leangsros
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!Jakub Kubrynski
 
OAuth2 and Spring Security
OAuth2 and Spring SecurityOAuth2 and Spring Security
OAuth2 and Spring SecurityOrest Ivasiv
 
REST with Spring Boot #jqfk
REST with Spring Boot #jqfkREST with Spring Boot #jqfk
REST with Spring Boot #jqfkToshiaki Maki
 

Destacado (20)

Spring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
Spring I/O 2012: Natural Templating in Spring MVC with ThymeleafSpring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
Spring I/O 2012: Natural Templating in Spring MVC with Thymeleaf
 
Shootout! template engines on the jvm
Shootout! template engines on the jvmShootout! template engines on the jvm
Shootout! template engines on the jvm
 
Thymeleaf, will it blend?
Thymeleaf, will it blend?Thymeleaf, will it blend?
Thymeleaf, will it blend?
 
Spring Framework - MVC
Spring Framework - MVCSpring Framework - MVC
Spring Framework - MVC
 
Shootout! Template engines for the JVM
Shootout! Template engines for the JVMShootout! Template engines for the JVM
Shootout! Template engines for the JVM
 
Website design configuration
Website design configurationWebsite design configuration
Website design configuration
 
App Design + Layout
App Design + LayoutApp Design + Layout
App Design + Layout
 
Thymeleafのすすめ
ThymeleafのすすめThymeleafのすすめ
Thymeleafのすすめ
 
Spring User Group Thymeleaf 08-21-2013
Spring User Group Thymeleaf 08-21-2013Spring User Group Thymeleaf 08-21-2013
Spring User Group Thymeleaf 08-21-2013
 
DSpace UI Prototype Challenge: Spring Boot + Thymeleaf
DSpace UI Prototype Challenge: Spring Boot + ThymeleafDSpace UI Prototype Challenge: Spring Boot + Thymeleaf
DSpace UI Prototype Challenge: Spring Boot + Thymeleaf
 
Java™ in Web 2.0
Java™ in Web 2.0Java™ in Web 2.0
Java™ in Web 2.0
 
Modern Java web applications with Spring Boot and Thymeleaf
Modern Java web applications with Spring Boot and ThymeleafModern Java web applications with Spring Boot and Thymeleaf
Modern Java web applications with Spring Boot and Thymeleaf
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Introduction to JPA Framework
Introduction to JPA FrameworkIntroduction to JPA Framework
Introduction to JPA Framework
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!
 
OAuth2 and Spring Security
OAuth2 and Spring SecurityOAuth2 and Spring Security
OAuth2 and Spring Security
 
REST with Spring Boot #jqfk
REST with Spring Boot #jqfkREST with Spring Boot #jqfk
REST with Spring Boot #jqfk
 
Spring Boot Tutorial
Spring Boot TutorialSpring Boot Tutorial
Spring Boot Tutorial
 

Similar a Thymeleaf Introduction

Servlets Java Slides & Presentation
Servlets Java Slides & Presentation Servlets Java Slides & Presentation
Servlets Java Slides & Presentation Anas Sa
 
Twig, the flexible, fast, and secure template language for PHP
Twig, the flexible, fast, and secure template language for PHPTwig, the flexible, fast, and secure template language for PHP
Twig, the flexible, fast, and secure template language for PHPFabien Potencier
 
Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress Maurizio Pelizzone
 
vJUG - The JavaFX Ecosystem
vJUG - The JavaFX EcosystemvJUG - The JavaFX Ecosystem
vJUG - The JavaFX EcosystemAndres Almiray
 
Core data intermediate Workshop at NSSpain 2013
Core data intermediate Workshop at NSSpain 2013Core data intermediate Workshop at NSSpain 2013
Core data intermediate Workshop at NSSpain 2013Diego Freniche Brito
 
Performance Tuning Your Puppet Infrastructure - PuppetConf 2014
Performance Tuning Your Puppet Infrastructure - PuppetConf 2014Performance Tuning Your Puppet Infrastructure - PuppetConf 2014
Performance Tuning Your Puppet Infrastructure - PuppetConf 2014Puppet
 
Twig: Friendly Curly Braces Invade Your Templates!
Twig: Friendly Curly Braces Invade Your Templates!Twig: Friendly Curly Braces Invade Your Templates!
Twig: Friendly Curly Braces Invade Your Templates!Ryan Weaver
 
.NET @ apache.org
 .NET @ apache.org .NET @ apache.org
.NET @ apache.orgTed Husted
 
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)Ondřej Machulda
 
Rapid Prototyping FTW!!!
Rapid Prototyping FTW!!!Rapid Prototyping FTW!!!
Rapid Prototyping FTW!!!cloudbring
 
Puppet at Bazaarvoice
Puppet at BazaarvoicePuppet at Bazaarvoice
Puppet at BazaarvoicePuppet
 
Apache Zeppelin and Helium @ApacheCon 2017 may, FL
Apache Zeppelin and Helium  @ApacheCon 2017 may, FLApache Zeppelin and Helium  @ApacheCon 2017 may, FL
Apache Zeppelin and Helium @ApacheCon 2017 may, FLAhyoung Ryu
 

Similar a Thymeleaf Introduction (20)

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

Último

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 

Último (20)

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

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