没有合适的资源?快使用搜索试试~ 我知道了~
首页thymeleaf pdf
thymeleaf pdf
1星 需积分: 13 13 下载量 48 浏览量
更新于2023-06-04
评论
收藏 502KB PDF 举报
thymeleaf 文档 hymeleaf提供了一个用于整合Spring MVC的可选模块,在应用开发中,你可以使用Thymeleaf来完全代替JSP,或其他模板引擎,如Velocity、FreeMarker等。Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式,因此也可以用作静态建模。你可以使用它创建经过验证的XML与HTML模板。相对于编写逻辑或代码,开发者只需将标签属性添加到模板中即可。接下来,这些标签属性就会在DOM(文档对象模型)上执行预先制定好的逻辑。
资源详情
资源评论
资源推荐
1 Introducing Thymeleaf
1.1 What is Thymeleaf?
Thymeleaf is a Java library. It is an XML/XHTML/HTML5 template engine able to apply a set of transformations to
template files in order to dis play data and/or text produced by your applications.
It is better suited for serving XHTML/HTML5 in web applications, but it can proces s any XML file, be it in web or in
standalone applications.
The main goal of Thymeleaf is to provide an eleg ant and well-formed way of creating templates . In order to achieve
this, it is based on XML tag s and attributes that define the execution of predefined log ic on the DOM (Document Object
Model), instead of explicitly writing that log ic as code inside the template.
Its architecture allows a fas t processing of templates, relying on intelligent caching of parsed files in order to use the
least poss ible amount of I/O operations during execution.
And last but not leas t, Thymeleaf has been designed from the beg inning with XML and Web standards in mind,
allowing you to create fully validating templates if that is a need for you.
1.2 What kind of templates can Thymeleaf process?
Out-of-the-box, Thymeleaf allows you to proces s s ix kinds of templates, each of which is called a Template Mode:
XML
Valid XML
XHTML
Valid XHTML
HTML5
Leg acy HTML5
All of thes e modes refer to well-formed XML files except the Legacy HTML5 mode, which allows you to proces s HTML5
files with features such as s tandalone (not closed) tag s, tag attributes without a value or not written between quotes .
In order to proces s files in this specific mode, Thymeleaf will first perform a transformation that will convert your files
to well-formed XML files which are still perfectly valid HTML5 (and are in fact the recommended way to create HTML5
code) .
Also note that validation is only available for XML and XHTML templates.
Nevertheless , these are not the only types of template that Thymeleaf can proces s, and the user is always able to
define his/her own mode by specifying both a way to parse templates in this mode and a way to write the results. This
way, anything that can be modelled as a DOM tree (be it XML or not) could effectively be processed as a template by
Thymeleaf.
1.3 Dialects: The Standard Dialect
Thymeleaf is an extremely extensible template engine (in fact it should be better called a template engine framework)
that allows you to completely define the DOM nodes that will be proces sed in your templates and als o how they will
be proces sed.
An object that applies some log ic to a DOM node is called a processor, and a s et of these proces sors —plus some
extra artifacts— is called a dialect, of which Thymeleaf’s core library provides one out-of-the-box called the Standard
Dialect, which should be enough for the needs of a big percent of users.
1
Page 2 of 76
The Standard Dialect is the dialect this tutorial covers. Every attribute and syntax feature you will learn about in the
following pag es is defined by this dialect, even if that isn’t explicitly mentioned.
Of cours e, users may create their own dialects (even extending the Standard one) if they want to define their own
proces sing logic while taking advantag e of the library’s advanced features . A Template Engine can be configured
several dialects at a time.
The official thymeleaf-spring 3 and thymeleaf-spring 4 integ ration packag es both define a dialect called
the “Spring Standard Dialect”, mostly equivalent to the Standard Dialect but with small adaptations to
make better use of some features in Spring Framework (for example, by using Spring Expression
Lang uag e instead of Thymeleaf’s standard OGNL). So if you are a Spring MVC user you are not wasting
your time, as almost everything you learn here will be of use in your Spring applications.
The Thymeleaf Standard Dialect can proces s templates in any mode, but is es pecially suited for web-oriented
template modes (XHTML and HTML5 ones). Besides HTML5, it specifically supports and validates the following XHTML
specifications: XHTML 1.0 Transitional, XHTML 1.0 Strict, XHTML 1.0 Frameset, and XHTML 1.1.
Mos t of the proces sors of the Standard Dialect are attribute processors. This allows browsers to correctly display
XHTML/HTML5 template files even before being processed, because they will simply ig nore the additional attributes.
For example, while a JSP using tag libraries could include a frag ment of code not directly displayable by a browser like:
<form:inputText name="userName" value="${user.name}" />
…the Thymeleaf Standard Dialect would allow us to achieve the same functionality with:
<input type="text" name="userName" value="James Carrot" th:value="${user.name}" />
Which not only will be correctly dis played by browsers , but also allow us to (optionally) specify a value attribute in it
(“James Carrot”, in this case) that will be displayed when the prototype is statically opened in a browser, and that will
be substituted by the value res ulting from the evaluation of ${user.name} during Thymeleaf proces sing of the
template.
If needed, this will allow your desig ner and developer to work on the very same template file and reduce the effort
required to transform a static prototype into a working template file. The ability to do this is a feature usually called
Natural Templating.
1.4 Overall Architecture
Thymeleaf’s core is a DOM processing engine. Specifically, it us es its own high-performance DOM implementation —
not the standard DOM API— for building in-memory tree representations of your templates , on which it later operates
by traversing their nodes and executing proces sors on them that modify the DOM according to the current
configuration and the s et of data that is pas sed to the template for its representation —known as the context.
The use of a DOM template representation makes it very well suited for web applications because web documents are
very often represented as object trees (in fact DOM trees are the way browsers represent web pag es in memory).
Also, building on the idea that mos t web applications use only a few dozen templates, that these are not big files and
that they don’t normally change while the application is running , Thymeleaf’s usag e of an in-memory cache of parsed
template DOM trees allows it to be fast in production environments, because very little I/O is needed (if any) for most
template proces sing operations.
If you want more detail, later in this tutorial there is an entire chapter dedicated to caching and to the
way Thymeleaf optimizes memory and resource usag e for faster operation.
Nevertheless , there is a res triction: this architecture also requires the use of big g er amounts of memory space for
Page 3 of 76
each template execution than other template parsing/proces sing approaches , which means that you should not us e
the library for creating big data XML documents (as opposed to web documents). As a general rule of thumb (and
always depending on the memory size of your JVM), if you are generating XML files with sizes around the tens of
meg abytes in a single template execution, you probably should not be using Thymeleaf.
The reason we consider this restriction only applies to data XML files and not web XHTML/HTML5 is that
you should never g enerate web documents so big that your users’ browsers set ablaze and/or explode
– remember that these browsers will also have to create DOM trees for your pag es!
1.5 Before going any further, you should read…
Thymeleaf is es pecially suited for working in web applications. And web applications are based on a series of
standards that everyone should know very well but few do – even if they have been working with them for years.
With the advent of HTML5, the state of the art in web standards today is more confusing than ever… are we going back
from XHTML to HTML? Will we abandon XML syntax? Why is nobody talking about XHTML 2.0 anymore?
So before going any further in this tutorial, you are strong ly advised to read an article on Thymeleaf’s web site called
“From HTML to HTML (via HTML)”, which you can find at this address :
http://www.thymeleaf.org /fromhtmltohtmlviahtml.html
Page 4 of 76
2 The Good Thymes Virtual Grocery
2.1 A website for a grocery
In order to better explain the concepts involved in proces sing templates with Thymeleaf, this tutorial will us e a demo
application you can download from the project web site.
This application represents the web site of an imag inary virtual g rocery, and will provide us with the adequate
scenarios to exemplify diverse Thymeleaf features.
We will need a quite simple set of model entities for our application: Products which are sold to Customers by
creating Orders . We will also be manag ing Comments about thos e Products :
Example application model
Our small application will als o have a very simple service layer, composed by Service objects containing methods
like:
public class ProductService {
Page 5 of 76
剩余75页未读,继续阅读
xueleisanmao
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1