Package org.thymeleaf

Class TemplateSpec

  • All Implemented Interfaces:
    Serializable

    public final class TemplateSpec
    extends Object
    implements Serializable

    Specification class containing everything needed by the template engine related to the template to be processed. Objects of this class are normally used as an argument to the different process(...) methods at ITemplateEngine.

    The only required value in a template specification is the template, which normally represents the template name, but can be the entire template contents if the template is meant to be specified as a String and resolved by a StringTemplateResolver.

    This is not to be mistaken for the template processing context, containing the data to be used during the processing of the template (variables, locale, etc.) and modelled by the IContext interface.

    The data contained in a Template Specification relates to and identifies the template itself, independently of any data (variables, etc.) used for processing it.

    Objects of this class are thread-safe.

    Since:
    3.0.0
    Author:
    Daniel Fernández
    See Also:
    Serialized Form
    • Constructor Detail

      • TemplateSpec

        public TemplateSpec​(String template,
                            TemplateMode templateMode)

        Build a new object of this class, specifying template and also template mode.

        The template normally represents the template name, but can be the entire template contents if the template is meant to be specified as a String and resolved by a StringTemplateResolver.

        The template mode only needs to be specified in cases when we want to force a template mode to be used for a template, independently of the mode that is selected for it by the configured template resolvers.

        This constructor will set no template selectors or template resolution attributes.

        Parameters:
        template - the template (usually the template name), required.
        templateMode - the template mode to be forced, can be null.
      • TemplateSpec

        public TemplateSpec​(String template,
                            String outputContentType)

        Build a new object of this class, specifying template and also output content type (MIME type). Most of the times this will force a template mode for template execution (e.g. text/html, application/javascript), but not always (e.g. text/event-stream).

        The template normally represents the template name, but can be the entire template contents if the template is meant to be specified as a String and resolved by a StringTemplateResolver.

        Supported relations between template mode and output content type are:

        • HTML: text/html, application/xhtml+xml
        • XML: application/xml
        • JAVASCRIPT: application/javascript, application/x-javascript, application/ecmascript, text/javascript, text/ecmascript, application/json
        • CSS: text/css
        • TEXT: text/plain

        The text/event-stream content type will also be supported, but will have no effect in forcing a template mode. Instead, it will put the engine into Server-Sent Event (SSE) output mode.

        Note content type parameters will be ignored (only the mime type itself will be used).

        This constructor will set no template selectors or template resolution attributes.

        Parameters:
        template - the template (usually the template name), required.
        outputContentType - the expected output content type, can be null.
        Since:
        3.0.6
      • TemplateSpec

        public TemplateSpec​(String template,
                            Map<String,​Object> templateResolutionAttributes)

        Build a new object of this class, specifying template and also template mode.

        The template normally represents the template name, but can be the entire template contents if the template is meant to be specified as a String and resolved by a StringTemplateResolver.

        The template resolution attributes are meant to be passed to the template resolvers (see ITemplateResolver during template resolution, as a way of configuring their execution for the template being processed.

        Note that template resolution attributes are considered a part of the identifier of a template, so they will be used as a part of the keys for cached templates. It is therefore required that template attribute maps contain values with valid equals(Object) and hashCode() implementations. Therefore, using simple (and fast) Map<String,String> maps is the recommended option.

        This constructor will set no template selectors or forced template mode.

        Parameters:
        template - the template (usually the template name), required.
        templateResolutionAttributes - the template resolution attributes, can be null.
      • TemplateSpec

        public TemplateSpec​(String template,
                            Set<String> templateSelectors,
                            TemplateMode templateMode,
                            Map<String,​Object> templateResolutionAttributes)

        Build a new object of this class, specifying a template mode that should be forced for template execution, ignoring the mode resolved by template resolvers.

        The template usually represents the template name, but can be the entire template contents if the template is meant to be specified as a String and resolved by a StringTemplateResolver.

        Template selectors allow the possibility to process only a part of the specified template, expressing this selection in a syntax similar to jQuery, CSS or XPath selectors. Note this is only available for markup template modes (HTML, XML). For more info on template selectors syntax, have a look at AttoParser's markup selectors documentation.

        The template mode only needs to be specified in cases when we want to force a template mode to be used for a template, independently of the mode that is selected for it by the configured template resolvers.

        The template resolution attributes are meant to be passed to the template resolvers (see ITemplateResolver during template resolution, as a way of configuring their execution for the template being processed.

        Note that template resolution attributes are considered a part of the identifier of a template, so they will be used as a part of the keys for cached templates. It is therefore required that template attribute maps contain values with valid equals(Object) and hashCode() implementations. Therefore, using simple (and fast) Map<String,String> maps is the recommended option.

        Parameters:
        template - the template (usually the template name), required.
        templateSelectors - the template selectors to be applied on the template.
        templateMode - the template mode to be forced, can be null.
        templateResolutionAttributes - the template resolution attributes, can be null.
      • TemplateSpec

        public TemplateSpec​(String template,
                            Set<String> templateSelectors,
                            String outputContentType,
                            Map<String,​Object> templateResolutionAttributes)

        Build a new object of this class, specifying an output content type (MIME type). Most of the times this will force a template mode for template execution (e.g. text/html, application/javascript), but not always (e.g. text/event-stream).

        The template usually represents the template name, but can be the entire template contents if the template is meant to be specified as a String and resolved by a StringTemplateResolver.

        Template selectors allow the possibility to process only a part of the specified template, expressing this selection in a syntax similar to jQuery, CSS or XPath selectors. Note this is only available for markup template modes (HTML, XML). For more info on template selectors syntax, have a look at AttoParser's markup selectors documentation.

        The template resolution attributes are meant to be passed to the template resolvers (see ITemplateResolver during template resolution, as a way of configuring their execution for the template being processed.

        Supported relations between template mode and output content type are:

        • HTML: text/html, application/xhtml+xml
        • XML: application/xml
        • JAVASCRIPT: application/javascript, application/x-javascript, application/ecmascript, text/javascript, text/ecmascript, application/json
        • CSS: text/css
        • TEXT: text/plain

        The text/event-stream content type will also be supported, but will have no effect in forcing a template mode. Instead, it will put the engine into Server-Sent Event (SSE) output mode.

        Note content type parameters will be ignored (only the mime type itself will be used).

        Note that template resolution attributes are considered a part of the identifier of a template, so they will be used as a part of the keys for cached templates. It is therefore required that template attribute maps contain values with valid equals(Object) and hashCode() implementations. Therefore, using simple (and fast) Map<String,String> maps is the recommended option.

        Parameters:
        template - the template (usually the template name), required.
        templateSelectors - the template selectors to be applied on the template.
        outputContentType - the expected output content type, can be null.
        templateResolutionAttributes - the template resolution attributes, can be null.
        Since:
        3.0.6
    • Method Detail

      • getTemplate

        public String getTemplate()

        Returns the template (usually the template name).

        This template normally represents the template name, but can be the entire template contents if the template is meant to be specified as a String and resolved by a StringTemplateResolver.

        Returns:
        the template. Cannot be null.
      • hasTemplateSelectors

        public boolean hasTemplateSelectors()

        Returns whether this spec has template selectors specified or not.

        Returns:
        true of there are template selectors, false if not.
      • getTemplateSelectors

        public Set<String> getTemplateSelectors()

        Returns the template selectors, if there are any.

        Template selectors allow the possibility to process only a part of the specified template, expressing this selection in a syntax similar to jQuery, CSS or XPath selectors. Note this is only available for markup template modes (HTML, XML). For more info on template selectors syntax, have a look at AttoParser's markup selectors documentation.

        Returns:
        the template selectors, or null if there are none.
      • hasTemplateMode

        public boolean hasTemplateMode()

        Returns whether this spec has template mode specified or not.

        Returns:
        true of there is a template mode, false if not.
      • getTemplateMode

        public TemplateMode getTemplateMode()

        Returns the template mode, if it has been specified.

        The template mode only needs to be specified in cases when we want to force a template mode to be used for a template, independently of the mode that is selected for it by the configured template resolvers.

        Returns:
        the template mode specified, or null if there isn't any.
      • hasTemplateResolutionAttributes

        public boolean hasTemplateResolutionAttributes()

        Returns whether this spec includes template resolution attributes or not.

        Returns:
        true of there are template resolution attributes, false if not.
      • getTemplateResolutionAttributes

        public Map<String,​Object> getTemplateResolutionAttributes()

        Returns the template resolution attributes, if any have been specified.

        The template resolution attributes are meant to be passed to the template resolvers (see ITemplateResolver during template resolution, as a way of configuring their execution for the template being processed.

        Note that template resolution attributes are considered a part of the identifier of a template, so they will be used as a part of the keys for cached templates. It is therefore required that template attribute maps contain values with valid equals(Object) and hashCode() implementations. Therefore, using simple (and fast) Map<String,String> maps is the recommended option.

        Returns:
        the template resolution attributes.
      • getOutputContentType

        public String getOutputContentType()

        Returns the output content type (MIME type). Most of the times this will force a template mode for template execution (e.g. text/html, application/javascript), but not always (e.g. text/event-stream).

        Supported relations between template mode and output content type are:

        • HTML: text/html, application/xhtml+xml
        • XML: application/xml
        • JAVASCRIPT: application/javascript, application/x-javascript, application/ecmascript, text/javascript, text/ecmascript, application/json
        • CSS: text/css
        • TEXT: text/plain

        The text/event-stream content type will also be supported, but will have no effect in forcing a template mode. Instead, it will put the engine into Server-Sent Event (SSE) output mode.

        Note content type parameters will be ignored (only the mime type itself will be used).

        Returns:
        the output content type, or null if none was specified.
      • isOutputSSE

        public boolean isOutputSSE()

        Returns whether output should be Server-Sent Events (SSE) or not.

        Server-Sent Events mode is enabled by setting the text/event-stream mime type as *output content type* constructor argument.

        Returns:
        true if output is supposed to be done via Server-Sent Events (SSE), false if not.
        Since:
        3.0.6
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object