Thymeleaf dialect and processor introduction

Source: Internet
Author: User
Tags processing instruction

Thymeleaf is an easy-to-extend library, and most user-oriented features are not built directly into his core, but are packaged and assembled into a collection of functions called dialect (dialects).
Due to the Spring-boot indirect type of the main push thymeleaf template, so this article mainly introduces the thymeleaf of some of the extended functions, although the performance of THYMELEAF3 compared to 2 of the increase is very large, but compared to other, there is a little difference.
dialect (dialects)

If we have studied some of the basics of thymeleaf, we should be able to realize that what we have previously known is not thymeleaf, but Thymeleaf's standard dialect, such as Th:text, which is just a standard dialect that can be used immediately.
We can also customize a set of attribute or tags to work with our templates in Thymeleaf.

Dialects is the object that implements the Org.thymeleaf.dialect.IDialect interface, as follows:

Public interface Idialect {public

    String getName ();

}

At the same time, the most basic interfaces are:
* Iprocessordialect Processor dialect
* Ipreprocessordialect preprocessing dialect
* Ipostprocessordialect post-processing dialect
* Iexpressionobjectdialect Expression Object dialect
* Iexecutionattributedialect executable Properties dialect Iprocessordialect processor dialect

See Interface code

Public interface Iprocessordialect extends Idialect {public
    String getprefix ();
    public int getdialectprocessorprecedence ();
    Public set<iprocessor> getprocessors (final String dialectprefix);

Processor is the object responsible for executing most of the logic in the Thymeleaf template. Also the most important extension dialect.
Three are defined primarily as attribute methods.
* Prefix applies to prefixes that match elements and attributes, similar to th in Th:if,thltext. If you want the processor to execute on an undefined label or property, you can define prefix as null
* Getdialectprocessorprecedence defines the priority of the dialect.
* Getprocessors defines a set of processors provided by this dialect. Ipreprocessordialect preprocessing dialect

Public interface Ipreprocessordialect extends Idialect {public
    int getdialectpreprocessorprecedence ();
    Public set<ipreprocessor> getpreprocessors ();
}

preprocessing and post-processing are different from processors. The processor is executed at a single time or on a template fragment. preprocessing and post-processing, as an additional step in the engine processing process, are applied throughout the execution of the template.
So they follow the different APIs that are done with the processor, and they are more event-oriented.
Preprocessing is applied in a specific case before the processor is executed for a specific template. The post processor is instead applied after the processor is executed. Ipostprocessordialec post-processing dialect

Public interface Ipostprocessordialect extends Idialect {public
    int getdialectpostprocessorprecedence ();
    Public set<ipostprocessor> getpostprocessors ();
}
Iexpressionobjectdialect Expression Object dialect

By implementing this interface, dialect can provide a new expression object or an expression application object, such as #strings, #numbers等

Public interface Iexpressionobjectdialect extends Idialect {public
    iexpressionobjectfactory Getexpressionobjectfactory ();
}

As you can see from the code, IEXPRESSIONOBJECTDIALECT returns a factory class because some expression objects need to process the data in the context to be built, so it's not possible to build them until we actually process the template. In addition, most expressions do not require expression objects, so they can be built on demand only when certain expressions are really needed.

Public interface Iexpressionobjectfactory {public
    map<string,expressionobjectdefinition> Getobjectdefinitions ();
    Public Object buildobject (final Iprocessingcontext processingcontext, final String expressionobjectname);
Iexecutionattributedialect executable attribute dialect

The dialect that implements this interface is allowed to provide execution properties, which are objects that are available to each processor during template processing.
For example, Standarddialect implements this interface to provide each processor with the following features:
* Thymeleaf Standard expression parser so that you can parse and execute standard expressions in any property
* Variable Expression calculator ... Expressions are executed in OGNL or Springel (depending on whether we use the Spring Integration module). ∗ in {...} Expressions are executed in OGNL or Springel (depending on whether we use the Spring Integration module). * in {{...}} Transform service to perform transformation operations in an expression
Note that these objects are not available in the context, so they cannot be used in template expressions. Their availability is limited to the implementation of extension points, such as processors, preprocessor, and so on.

Public interface Iexecutionattributedialect extends Idialect {public
    map<string,object> Getexecutionattributes ();
}
Processors Processor

The processor's objects all implement the Org.thymeleaf.processor.IProcessor interface.
The interface code is as follows:

Public interface Iprocessor {public
    templatemode gettemplatemode ();
    public int getprecedence ();
}

The next several common types of processor Ielementprocessor are described below

The element processor is executed on an open element or on a separate element.

Public interface Ielementprocessor extends Iprocessor {

    /**
     * <p>
     *   Returns the element name that W Ould make this processor match (if any).
     * </p>
     *
     * @return The element name.
     *
    /Public matchingelementname getmatchingelementname ();

    /**
     * <p>
     *   Returns the attribute name is would make this processor match (if any).
     * </p>
     *
     * @return the attribute name.
     *
    /Public matchingattributename getmatchingattributename ();

}

The Ielementprocessor processor does not directly implement this interface, it also contains two sub-interfaces
* Ielementtagprocessor
* Ielementmodelprocessor itemplateboundariesprocessor Other processors

The following are other events where Thymeleaf 3.0 allows the declaration of the processor, each of which implements the appropriate interface:
* Text Events: interface Itextprocessor
* Comment Events: interface Icommentprocessor
* CDATA Section events: Interface Icdatasectionprocessor
* DOCTYPE Clause Events: interface Idoctypeprocessor
* XML Declaration Events: interface Ixmldeclarationprocessor
* Processing instruction Events: interface Iprocessinginstructionprocessor

The processor is not described in detail, followed by specific practical use to illustrate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.