Class SpringTemplateEngine
- All Implemented Interfaces:
org.springframework.beans.factory.Aware,org.springframework.context.MessageSourceAware,org.thymeleaf.ITemplateEngine,ISpringTemplateEngine
- Direct Known Subclasses:
SpringWebFluxTemplateEngine
Implementation of ISpringTemplateEngine meant for Spring-enabled applications,
that establishes by default an instance of SpringStandardDialect
as a dialect (instead of an instance of StandardDialect.
It also configures a SpringMessageResolver as message resolver (backed by
Spring's own MessageSource infrastructure), and implements the
MessageSourceAware interface in order to let Spring automatically set the
MessageSource used at the application (bean needs to have id
"messageSource"). If this Spring standard setting needs to be overridden,
the setTemplateEngineMessageSource(MessageSource) method can be used.
Message resolver vs. message source — precedence rule: if a custom message
resolver is explicitly configured on this engine by calling
setMessageResolver(IMessageResolver), setMessageResolvers(Set), or
addMessageResolver(IMessageResolver), then the automatic setup of
SpringMessageResolver is skipped entirely, and any value set via
setMessageSource(MessageSource) or
setTemplateEngineMessageSource(MessageSource) is ignored. Explicitly
configured message resolvers always take precedence over message sources.
- Since:
- 3.0.3
- Author:
- Daniel Fernández
-
Field Summary
Fields inherited from class org.thymeleaf.TemplateEngine
TIMER_LOGGER_NAME -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddMessageResolver(org.thymeleaf.messageresolver.IMessageResolver messageResolver) Adds a message resolver to the existing set, suppressing the automatic configuration ofSpringMessageResolverorStandardMessageResolverthat would otherwise happen during engine initialization.Collection<Class<?>>Returns the classes that will be allowed to be used in SpEL expressions in views, explicitly overriding the standard set of forbidden classes.booleanReturns whether the SpringEL compiler should be enabled in SpringEL expressions or not.booleanReturns whether the<input type="hidden" ...>marker tags rendered to signal the presence of checkboxes in forms when unchecked should be rendered before the checkbox tag itself, or after (default).protected final voidprotected voidThis method performs additional initializations required for aSpringTemplateEnginesubclass instance.voidsetAllowedClassOverridesForViews(Collection<Class<?>> allowedClassOverridesForViews) Sets the collection of classes that will be explicitly allowed to be used in SpEL expressions in views, overriding the standard set of forbidden classes.voidsetEnableSpringELCompiler(boolean enableSpringELCompiler) Sets whether the SpringEL compiler should be enabled in SpringEL expressions or not.voidsetMessageResolver(org.thymeleaf.messageresolver.IMessageResolver messageResolver) Sets a single message resolver, suppressing the automatic configuration ofSpringMessageResolverorStandardMessageResolverthat would otherwise happen during engine initialization.voidsetMessageResolvers(Set<org.thymeleaf.messageresolver.IMessageResolver> messageResolvers) Sets the full set of message resolvers, suppressing the automatic configuration ofSpringMessageResolverorStandardMessageResolverthat would otherwise happen during engine initialization.voidsetMessageSource(org.springframework.context.MessageSource messageSource) Implementation of theMessageSourceAware.setMessageSource(MessageSource)method at theMessageSourceAwareinterface, provided so that Spring is able to automatically set the currently configuredMessageSourceinto this template engine.voidsetRenderHiddenMarkersBeforeCheckboxes(boolean renderHiddenMarkersBeforeCheckboxes) Sets whether the<input type="hidden" ...>marker tags rendered to signal the presence of checkboxes in forms when unchecked should be rendered before the checkbox tag itself, or after (default).voidsetTemplateEngineMessageSource(org.springframework.context.MessageSource templateEngineMessageSource) Convenience method for setting the message source that will be used by this template engine, overriding the one automatically set by Spring at thesetMessageSource(MessageSource)method.Methods inherited from class org.thymeleaf.TemplateEngine
addDialect, addDialect, addLinkBuilder, addTemplateResolver, clearDialects, clearTemplateCache, clearTemplateCacheFor, getCacheManager, getConfiguration, getDecoupledTemplateLogicResolver, getDialects, getDialectsByPrefix, getEngineContextFactory, getLinkBuilders, getMessageResolvers, getTemplateResolvers, isInitialized, process, process, process, process, process, process, processThrottled, processThrottled, processThrottled, setAdditionalDialects, setCacheManager, setDecoupledTemplateLogicResolver, setDialect, setDialects, setDialectsByPrefix, setEngineContextFactory, setLinkBuilder, setLinkBuilders, setTemplateResolver, setTemplateResolvers, threadIndexMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.thymeleaf.ITemplateEngine
getConfiguration, process, process, process, process, process, process, processThrottled, processThrottled, processThrottled
-
Constructor Details
-
SpringTemplateEngine
public SpringTemplateEngine()
-
-
Method Details
-
setMessageSource
public void setMessageSource(org.springframework.context.MessageSource messageSource) Implementation of the
MessageSourceAware.setMessageSource(MessageSource)method at theMessageSourceAwareinterface, provided so that Spring is able to automatically set the currently configuredMessageSourceinto this template engine.If several
MessageSourceimplementation beans exist, Spring will inject here the one with id"messageSource".This property should not be set manually in most scenarios (see
setTemplateEngineMessageSource(MessageSource)instead).Note that this setting is only used when no explicit message resolver has been configured via
setMessageResolver(IMessageResolver),setMessageResolvers(Set), oraddMessageResolver(IMessageResolver). If an explicit resolver is set, this message source is ignored (see class-level Javadoc for the full precedence rule).- Specified by:
setMessageSourcein interfaceorg.springframework.context.MessageSourceAware- Parameters:
messageSource- the message source to be used by the message resolver
-
setTemplateEngineMessageSource
public void setTemplateEngineMessageSource(org.springframework.context.MessageSource templateEngineMessageSource) Convenience method for setting the message source that will be used by this template engine, overriding the one automatically set by Spring at the
setMessageSource(MessageSource)method.Note that this setting is only used when no explicit message resolver has been configured via
setMessageResolver(IMessageResolver),setMessageResolvers(Set), oraddMessageResolver(IMessageResolver). If an explicit resolver is set, this message source is ignored (see class-level Javadoc for the full precedence rule).- Specified by:
setTemplateEngineMessageSourcein interfaceISpringTemplateEngine- Parameters:
templateEngineMessageSource- the message source to be used by the message resolver
-
getAllowedClassOverridesForViews
Description copied from interface:ISpringTemplateEngineReturns the classes that will be allowed to be used in SpEL expressions in views, explicitly overriding the standard set of forbidden classes.
- Specified by:
getAllowedClassOverridesForViewsin interfaceISpringTemplateEngine- Returns:
- the classes that will be allowed to be used in expressions in views even if forbidden by default.
-
setAllowedClassOverridesForViews
Description copied from interface:ISpringTemplateEngineSets the collection of classes that will be explicitly allowed to be used in SpEL expressions in views, overriding the standard set of forbidden classes.
- Specified by:
setAllowedClassOverridesForViewsin interfaceISpringTemplateEngine- Parameters:
allowedClassOverridesForViews- a collection ofClass<?>instances representing the classes to be allowed in expressions within views.
-
getEnableSpringELCompiler
public boolean getEnableSpringELCompiler()Returns whether the SpringEL compiler should be enabled in SpringEL expressions or not.
(This is just a convenience method, equivalent to calling
SpringStandardDialect.getEnableSpringELCompiler()on the dialect instance itself. It is provided here in order to allow users to enable the SpEL compiler without having to directly create instances of theSpringStandardDialect)Expression compilation can significantly improve the performance of Spring EL expressions, but might not be adequate for every environment. Read the official Spring documentation for more detail.
Also note that although Spring includes a SpEL compiler since Spring 4.1, most expressions in Thymeleaf templates will only be able to properly benefit from this compilation step when at least Spring Framework version 4.2.4 is used.
This flag is set to
falseby default.- Returns:
trueif SpEL expressions should be compiled if possible,falseif not.
-
setEnableSpringELCompiler
public void setEnableSpringELCompiler(boolean enableSpringELCompiler) Sets whether the SpringEL compiler should be enabled in SpringEL expressions or not.
(This is just a convenience method, equivalent to calling
SpringStandardDialect.setEnableSpringELCompiler(boolean)on the dialect instance itself. It is provided here in order to allow users to enable the SpEL compiler without having to directly create instances of theSpringStandardDialect)Expression compilation can significantly improve the performance of Spring EL expressions, but might not be adequate for every environment. Read the official Spring documentation for more detail.
Also note that although Spring includes a SpEL compiler since Spring 4.1, most expressions in Thymeleaf templates will only be able to properly benefit from this compilation step when at least Spring Framework version 4.2.4 is used.
This flag is set to
falseby default.- Parameters:
enableSpringELCompiler-trueif SpEL expressions should be compiled if possible,falseif not.
-
getRenderHiddenMarkersBeforeCheckboxes
public boolean getRenderHiddenMarkersBeforeCheckboxes()Returns whether the
<input type="hidden" ...>marker tags rendered to signal the presence of checkboxes in forms when unchecked should be rendered before the checkbox tag itself, or after (default).(This is just a convenience method, equivalent to calling
SpringStandardDialect.getRenderHiddenMarkersBeforeCheckboxes()on the dialect instance itself. It is provided here in order to allow users to modify this behaviour without having to directly create instances of theSpringStandardDialect)A number of CSS frameworks and style guides assume that the
<label ...>for a checkbox will appear in markup just after the<input type="checkbox" ...>tag itself, and so the default behaviour of rendering an<input type="hidden" ...>after the checkbox can lead to bad application of styles. By tuning this flag, developers can modify this behaviour and make the hidden tag appear before the checkbox (and thus allow the lable to truly appear right after the checkbox).Note this hidden field is introduced in order to signal the existence of the field in the form being sent, even if the checkbox is unchecked (no URL parameter is added for unchecked check boxes).
This flag is set to
falseby default.- Returns:
trueif hidden markers should be rendered before the checkboxes,falseif not.- Since:
- 3.0.10
-
setRenderHiddenMarkersBeforeCheckboxes
public void setRenderHiddenMarkersBeforeCheckboxes(boolean renderHiddenMarkersBeforeCheckboxes) Sets whether the
<input type="hidden" ...>marker tags rendered to signal the presence of checkboxes in forms when unchecked should be rendered before the checkbox tag itself, or after (default).(This is just a convenience method, equivalent to calling
SpringStandardDialect.setRenderHiddenMarkersBeforeCheckboxes(boolean)on the dialect instance itself. It is provided here in order to allow users to modify this behaviour without having to directly create instances of theSpringStandardDialect)A number of CSS frameworks and style guides assume that the
<label ...>for a checkbox will appear in markup just after the<input type="checkbox" ...>tag itself, and so the default behaviour of rendering an<input type="hidden" ...>after the checkbox can lead to bad application of styles. By tuning this flag, developers can modify this behaviour and make the hidden tag appear before the checkbox (and thus allow the lable to truly appear right after the checkbox).Note this hidden field is introduced in order to signal the existence of the field in the form being sent, even if the checkbox is unchecked (no URL parameter is added for unchecked check boxes).
This flag is set to
falseby default.- Parameters:
renderHiddenMarkersBeforeCheckboxes-trueif hidden markers should be rendered before the checkboxes,falseif not.- Since:
- 3.0.10
-
setMessageResolver
public void setMessageResolver(org.thymeleaf.messageresolver.IMessageResolver messageResolver) Sets a single message resolver, suppressing the automatic configuration of
SpringMessageResolverorStandardMessageResolverthat would otherwise happen during engine initialization.Calling this method means any value set via
setMessageSource(MessageSource)orsetTemplateEngineMessageSource(MessageSource)will be ignored. See the class-level Javadoc for the full precedence rule.- Overrides:
setMessageResolverin classorg.thymeleaf.TemplateEngine- Parameters:
messageResolver- the message resolver to be set.
-
setMessageResolvers
public void setMessageResolvers(Set<org.thymeleaf.messageresolver.IMessageResolver> messageResolvers) Sets the full set of message resolvers, suppressing the automatic configuration of
SpringMessageResolverorStandardMessageResolverthat would otherwise happen during engine initialization.Calling this method means any value set via
setMessageSource(MessageSource)orsetTemplateEngineMessageSource(MessageSource)will be ignored. See the class-level Javadoc for the full precedence rule.- Overrides:
setMessageResolversin classorg.thymeleaf.TemplateEngine- Parameters:
messageResolvers- the set of message resolvers to be set.
-
addMessageResolver
public void addMessageResolver(org.thymeleaf.messageresolver.IMessageResolver messageResolver) Adds a message resolver to the existing set, suppressing the automatic configuration of
SpringMessageResolverorStandardMessageResolverthat would otherwise happen during engine initialization.Calling this method means any value set via
setMessageSource(MessageSource)orsetTemplateEngineMessageSource(MessageSource)will be ignored. See the class-level Javadoc for the full precedence rule.- Overrides:
addMessageResolverin classorg.thymeleaf.TemplateEngine- Parameters:
messageResolver- the message resolver to be added.
-
initializeSpecific
protected final void initializeSpecific()- Overrides:
initializeSpecificin classorg.thymeleaf.TemplateEngine
-
initializeSpringSpecific
protected void initializeSpringSpecific()This method performs additional initializations required for a
SpringTemplateEnginesubclass instance. This method is called before the first execution ofTemplateEngine.process(String, org.thymeleaf.context.IContext)orTemplateEngine.processThrottled(String, org.thymeleaf.context.IContext)in order to create all the structures required for a quick execution of templates.THIS METHOD IS INTERNAL AND SHOULD NEVER BE CALLED DIRECTLY.
The implementation of this method does nothing, and it is designed for being overridden by subclasses of
SpringTemplateEngine.
-