没有合适的资源?快使用搜索试试~ 我知道了~
Best Practices for Programming Eclipse and OSGi.pdf
需积分: 8 1 下载量 13 浏览量
2018-05-12
12:24:04
上传
评论
收藏 212KB PDF 举报
温馨提示
Best Practices for Programming Eclipse and OSGi.pdf Best Practices for Programming Eclipse and OSGi.pdf
资源推荐
资源详情
资源评论

















1 Some Reasons to Extend Thymeleaf
Thymeleaf is an extremely extensible library. The key to it is that mos t of its user-oriented features are not directly built
into its core, but rather just packag ed and componentized into feature sets called dialects.
The library offers you two dialects out-of-the-box: the Standard and the SpringStandard dialects, but you can easily
create your own. Let’s explore some of the reasons for doing this:
1.1. Scenario 1: adding features to the Standard dialects
Say your application uses the SpringStandard dialect and that it needs to show an alert text box in blue or red
background depending on the user’s role (admin or non-admin) from Monday to Saturday, but always in green on
Sundays. You can compute this with conditional expres sions on your template, but too many conditions could render
your code a little bit hard to read…
Solution: create a new attribute called alertclass and an attribute proces sor for it (Java code that will compute the
rig ht CSS clas s), and packag e it into your own MyOwnDialect dialect. Add this dialect to your template engine with the
th prefix (same as the SpringStandard one) and you’ll now be able to use th:alertclass="${user.role}" !
1.2. Scenario 2: view-layer components
Let’s say your company uses Thymeleaf extensively, and you want to create a repos itory of common functionalities
(tag s and/or attributes ) that you can use in several applications without having to copy-pas te them from one
application to the next. This is, you want to create view-layer components in a similar way to JSPs taglibs.
Solution: create a Thymeleaf dialect for each set of related functionalities, and add thes e dialects to your applications
as needed. Note that if the tag s or attributes in thes e dialects make use of externalized (internationalized) mes sages ,
you will be able to packag e these mess ag es along with your dialects (in the shape of processor messages) instead of
requiring that all of your applications include them in their mes sag es .properties files as you would with JSP.
1.3. Scenario 3: creating your own template system
Now imag ine your are creating a public webs ite that allows users to create their own des ig n templates for showing
their content. Of course, you don’t want your users to be able to do absolutely anything in their templates , not even all
that the Standard Dialect allows (for example, execute OGNL expres sions). So you need to offer your users the ability
to add to their templates only a very specific set of features that are under your control (like showing a profile photo,
a blog entry text, etc).
Solution: create a Thymeleaf dialect with the tag s or attributes you want your users to be able to use, like
<mysite:profilePhoto /> or <mysite:blogentries fromDate="23/4/2011" /> . Then allow your users to create
their own templates using thes e features and just let Thymeleaf execute them, being sure nobody will be doing what
they’re not allowed to.
Page 2 of 16

2 Dialects and Processors
2.1. Dialects
If you’ve read the Using Thymeleaf tutorial before g etting here —which you should have done—, you should know that
what you’ve been learning all this time was not exactly Thymeleaf, but rather its Standard Dialect (or the SpringStandard
Dialect, if you’ve also read the Thymeleaf + Spring tutorial).
What does that mean? It means that all those th:x attributes you learned to use are only a standard, out-of-the-box
set of features, but you can define your own set of attributes (or tag s) with the names you wish and use them in
Thymeleaf to proces s your templates . You can define your own dialects.
Dialects are objects implementing the org.thymeleaf.dialect.IDialect interface, which looks like this:
public interface IDialect {
public String getPrefix();
public Set<IProcessor> getProcessors();
public Map<String,Object> getExecutionAttributes();
public Set<IDocTypeTranslation> getDocTypeTranslations();
public Set<IDocTypeResolutionEntry> getDocTypeResolutionEntries();
}
Let’s see thes e methods step by step:
First, the prefix:
public String getPrefix();
This is the prefix that the tag s and attributes of your dialect will have, a kind of namespace (although it can be changed
when adding dialects to the Template Engine). If you create an attribute named earth and your dialect prefix is
planets , you will write this attribute in your templates as planets:earth .
The prefix for both the Standard and SpringStandard Dialects is, obviously, th . Prefix can be null s o that you can
define attribute/tag process ors for non-names paced tag s (for example, s tandard <p> , <div> or <table> tag s in
XHTML).
Now, let’s have a look at the most important part of the IDialect interface, the processors:
public Set<IProcessor> getProcessors();
Process ors are the objects in charg e of executing on DOM nodes and performing changes on it. We will cover
proces sors in more detail in next sections.
Execution attributes are objects that are contributed by the dialect to the execution arg uments during the proces sing of
templates . These are objects —usually utility objects— that will be made available to proces sors during their
execution. Note that these objects will not appear at the variable context, and will be only visible internally.
public Map<String,Object> getExecutionAttributes();
More interface methods:
public Set<IDocTypeTranslation> getDocTypeTranslations();
Page 3 of 16
剩余15页未读,继续阅读
资源评论


Nicky.Ma
- 粉丝: 2w+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
