擴(kuò)展Thymeleaf很容易:只需要?jiǎng)?chuàng)建一個(gè)方言并將其添加到模板引擎。 下面來看看,如何一步步地實(shí)現(xiàn)。
所有在這里看到的代碼都來自一個(gè)工作應(yīng)用程序??梢詮?a target="_blank" title="GitHub倉庫查看或下載源代碼">GitHub倉庫查看或下載源代碼。
Thymeleaf方言(Dialects)是可以在模板中使用的一組功能。 這些功能包括:
#array
,#dates
等),以執(zhí)行可能需要的操作。所有這些功能都是可選的,方言只能指定其中的一部分。 例如,一個(gè)方言可能不需要指定任何處理器,但是可以聲明幾個(gè)表達(dá)式對象。
如果已經(jīng)看到用標(biāo)準(zhǔn)方言編寫的代碼片段,應(yīng)該注意到,可處理的屬性以th:
開頭。 這個(gè)“th”
被稱為方言前綴,這意味著由該方言處理的所有標(biāo)簽和屬性將以這樣的前綴開頭。 每種方言都可以指定自己的前綴。
同樣重要的是要注意,一個(gè)模板引擎可以一次設(shè)置多個(gè)方言,從而允許處理包括來自所有指定方言的特征的模板(將方言看作是一種JSP標(biāo)簽庫)。 更重要的是,這些方言中的一些可以共享前綴,有效地作為一種方言。
這里將在應(yīng)用程序中創(chuàng)建一個(gè)方言。 這將是一個(gè)Spring MVC應(yīng)用程序,所以將要已經(jīng)使用SpringStandard方言(更多細(xì)節(jié)參見Thymeleaf + Spring教程)。 但是想添加一個(gè)新的屬性,向請求的客戶端顯示問候語,如下所示:
<p hello:sayto="World">Hi ya!</p>
首先,需要?jiǎng)?chuàng)建屬性處理器來處理顯問候語消息。
所有處理器都實(shí)現(xiàn)org.thymeleaf.processor.IProcessor
接口,特別是標(biāo)記處理器實(shí)現(xiàn)org.thymeleaf.processor.element.IElementTagProcessor
接口,因?yàn)樗且粋€(gè)處理器,它適用于元素(以XML/HTML術(shù)語),這種元素的開放標(biāo)簽。
另外,這個(gè)處理器會(huì)被這個(gè)開放標(biāo)簽(hello:sayto
)中的指定屬性觸發(fā),所以將擴(kuò)展一個(gè)有用的抽象類,它將給出大部分的類基礎(chǔ)結(jié)構(gòu):org.thymeleaf.processor.element.AbstractAttributeTagProcessor
。請參考下面代碼的實(shí)現(xiàn) -
public class SayToAttributeTagProcessor extends AbstractAttributeTagProcessor {
private static final String ATTR_NAME = "sayto";
private static final int PRECEDENCE = 10000;
public SayToAttributeTagProcessor(final String dialectPrefix) {
super(
TemplateMode.HTML, // This processor will apply only to HTML mode
dialectPrefix, // Prefix to be applied to name for matching
null, // No tag name: match any tag name
false, // No prefix to be applied to tag name
ATTR_NAME, // Name of the attribute that will be matched
true, // Apply dialect prefix to attribute name
PRECEDENCE, // Precedence (inside dialect's precedence)
true); // Remove the matched attribute afterwards
}
protected void doProcess(
final ITemplateContext context, final IProcessableElementTag tag,
final AttributeName attributeName, final String attributeValue,
final IElementTagStructureHandler structureHandler) {
structureHandler.setBody(
"Hello, " + HtmlEscape.escapeHtml5(attributeValue) + "!", false);
}
}
創(chuàng)建處理器非常簡單,但現(xiàn)在還需要?jiǎng)?chuàng)建方言類,負(fù)責(zé)告訴Thymeleaf處理器是可用的。
最基本的方言接口:org.thymeleaf.dialect.IDialect
只告訴Thymeleaf一個(gè)特定的類是方言。 但是引擎需要知道那個(gè)創(chuàng)建的方言能夠提供什么,并且聲明方言類,需要實(shí)現(xiàn)一組或幾組IDialect
子接口。
具體來說,out
方言將提供*
處理器,因此它將實(shí)現(xiàn)org.thymeleaf.dialect.IProcessorDialect
。 為了更容易一些,這里不是直接實(shí)現(xiàn)接口,而是擴(kuò)展一個(gè)名為org.thymeleaf.dialect.AbstractProcessorDialect
的抽象類,參考以下代碼:
public class HelloDialect extends AbstractProcessorDialect {
public HelloDialect() {
super(
"Hello Dialect", // Dialect name
"hello", // Dialect prefix (hello:*)
1000); // Dialect precedence
}
/*
* Initialize the dialect's processors.
*
* Note the dialect prefix is passed here because, although we set
* "hello" to be the dialect's prefix at the constructor, that only
* works as a default, and at engine configuration time the user
* might have chosen a different prefix to be used.
*/
public Set<IProcessor> getProcessors(final String dialectPrefix) {
final Set<IProcessor> processors = new HashSet<IProcessor>();
processors.add(new SayToAttributeTagProcessor(dialectPrefix));
return processors;
}
}
使用上面創(chuàng)建這個(gè)新方言非常簡單。 這是一個(gè)Spring MVC應(yīng)用程序,只需在配置期間將它添加到templateEngine
Bean。如下代碼所示 -
@Bean
public SpringTemplateEngine templateEngine(){
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setEnableSpringELCompiler(true);
templateEngine.setTemplateResolver(templateResolver());
templateEngine.addDialect(new HelloDialect());
return templateEngine;
}
請注意,通過使用addDialect()
,而不是setDialect()
,告訴引擎除了默認(rèn)的StandardDialect
之外,還想使用新的方言。 所以所有的標(biāo)準(zhǔn)th:*
屬性也將可用。
現(xiàn)在需要新屬性可以無縫工作,如下:
<p>Hello World!</p>