thymeleaf方言和處理器簡介

來源:互聯網
上載者:User

thymeleaf是一個很容易擴充的庫,大部分面向使用者的功能不是直接構建在他的核心中,而是通過打包和組件化到一個稱謂Dialect(方言)的功能集合中。
由於Spring-boot間接式的主推了thymeleaf模板,所以本文主要介紹下thymeleaf的一些擴充功能,儘管thymeleaf3的效能相比2來說提升了很大,但是相比別的,還是有一點差距。
方言(Dialects)

如果我們研究過thymeleaf的一些基礎使用的話,應該能意識到我們之前瞭解的準確的說不是thymeleaf,而是thymeleaf的標準方言,例如th:text,僅僅只是可以立即使用的標準方言。
同時我們可以自訂一組attribute或者tag在thymeleaf中用來處理我們的模板。

Dialects是實現了org.thymeleaf.dialect.IDialect介面的對象,具體如下:

public interface IDialect {    public String getName();}

同時最基礎的介面有:
* IProcessorDialect 處理器方言
* IPreProcessorDialect 預先處理方言
* IPostProcessorDialect 後處理方言
* IExpressionObjectDialect 運算式對象方言
* IExecutionAttributeDialect 可執行屬性方言 IProcessorDialect 處理器方言

參看介面代碼

public interface IProcessorDialect extends IDialect {    public String getPrefix();    public int getDialectProcessorPrecedence();    public Set<IProcessor> getProcessors(final String dialectPrefix);}

processor是負責執行thymeleaf模板中的大部分邏輯的對象。也是最重要的擴充方言。
定義了三個主要是屬性方法。
* prefix 應用於匹配元素和屬性的首碼,類似於th:if,thLtext中的th。如果希望處理器在未定義的標籤或者屬性上執行,則可以將prefix定義為null
* getDialectProcessorPrecedence 定義方言的優先順序。
* getProcessors 定義一組由該方言提供的處理器集合。 IPreProcessorDialect 預先處理方言

public interface IPreProcessorDialect extends IDialect {    public int getDialectPreProcessorPrecedence();    public Set<IPreProcessor> getPreProcessors();}

預先處理和後處理與處理器不同。處理器是在單個時間或者模板片段上執行。而預先處理和後處理是作為引擎處理過程中的附加步驟,應用在整個模板的執行過程中。
因此他們遵循與處理器完成不同的API,他們更加面向事件。
預先處理在特定的情況下,是在為特定模板執行處理器之前應用的。後處理器則相反,是在執行處理器之後應用。 IPostProcessorDialec 後處理方言

public interface IPostProcessorDialect extends IDialect {    public int getDialectPostProcessorPrecedence();    public Set<IPostProcessor> getPostProcessors();}
IExpressionObjectDialect 運算式對象方言

通過實現此介面,Dialect可以提供新的運算式對象或者運算式應用程式物件,例如#strings,#numbers等

public interface IExpressionObjectDialect extends IDialect {    public IExpressionObjectFactory getExpressionObjectFactory();}

通過代碼可以看出,IExpressionObjectDialect返回了一個工廠類,原因是一些運算式對象需要處理內容相關的資料才能被構建,所以在我們真正處理模板之前不可能構建他們。此外,大多數運算式並不需要運算式對象,所以只有在特定運算式真正需要的時候才能按需構建他們。

public interface IExpressionObjectFactory {    public Map<String,ExpressionObjectDefinition> getObjectDefinitions();    public Object buildObject(final IProcessingContext processingContext, final String expressionObjectName);}
IExecutionAttributeDialect 可執行屬性方言

實現這個介面的方言被允許提供執行屬性,即在模板處理期間執行的每個處理器可用的對象。
例如StandardDialect實現這個介面,以便為每個處理器提供以下功能:
* Thymeleaf標準運算式解析器,以便可以解析和執行任何屬性中的標準運算式
* Variant 運算式計算機 …運算式在OGNL或SpringEL中執行(取決於我們是否使用Spring整合模組)。∗在 {…}運算式在OGNL或SpringEL中執行(取決於我們是否使用Spring整合模組)。 * 在 {{…}}運算式中執行轉換操作的轉換服務
注意,這些對象在上下文中不可用,因此它們不能在模板運算式中使用。 它們的可用性僅限於擴充點的實現,例如處理器,前置處理器等。

public interface IExecutionAttributeDialect extends IDialect {    public Map<String,Object> getExecutionAttributes();}
Processors處理器

處理器的對象全部實現org.thymeleaf.processor.IProcessor介面。
介面代碼如下:

public interface IProcessor {    public TemplateMode getTemplateMode();    public int getPrecedence();}

接下來介紹下幾種常見類型的處理器 IElementProcessor

元素處理器是在open element 或者獨立元素上執行。

public interface IElementProcessor extends IProcessor {    /**     * <p>     *   Returns the element name that would make this processor match (if any).     * </p>     *     * @return the element name.     */    public MatchingElementName getMatchingElementName();    /**     * <p>     *   Returns the attribute name that would make this processor match (if any).     * </p>     *     * @return the attribute name.     */    public MatchingAttributeName getMatchingAttributeName();}

IElementProcessor處理器並不是直接實現這個介面,它還包含了兩個子介面
* IElementTagProcessor
* IElementModelProcessor ITemplateBoundariesProcessor 其他處理器

以下是Thymeleaf 3.0允許宣各處理器的其他事件,它們中的每一個都實現了相應的介面:
* Text events: 介面 ITextProcessor
* Comment events: 介面 ICommentProcessor
* CDATA Section events: 介面 ICDATASectionProcessor
* DOCTYPE Clause events: 介面 IDocTypeProcessor
* XML Declaration events: 介面 IXMLDeclarationProcessor
* Processing Instruction events: 介面 IProcessingInstructionProcessor

處理器並沒有詳細介紹,後續通過具體的實際使用來說明

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.