Class StringTemplateResolver
- All Implemented Interfaces:
ITemplateResolver
Implementation of ITemplateResolver that extends AbstractTemplateResolver
and creates StringTemplateResource instances for template resources.
This template resolvers will consider the template being resolved as the template itself,
this is, its contents. No external file or resource will be therefore accessed.
This template resolver will consider its resolved templates non-cacheable by default,
given its nature of being used for resolving arbitrary String objects.
Also, the TemplateMode.HTML template mode will be used by default.
Note this is the default template resolver that TemplateEngine
instances will use if no other resolvers are configured.
- Since:
- 3.0.0
- Author:
- Daniel Fernández
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final LongDefault value for the cache TTL: null.static final booleanDefault value for the cacheable flag: falsestatic final TemplateModeDefault template mode:TemplateMode.HTMLFields inherited from class org.thymeleaf.templateresolver.AbstractTemplateResolver
DEFAULT_EXISTENCE_CHECK, DEFAULT_USE_DECOUPLED_LOGIC -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected TemplateModecomputeTemplateMode(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String, Object> templateResolutionAttributes) Computes the template mode that should be applied to a template, according to existing configuration.protected ITemplateResourcecomputeTemplateResource(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String, Object> templateResolutionAttributes) Computes the resolved template resource.protected ICacheEntryValiditycomputeValidity(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String, Object> templateResolutionAttributes) Computes the validity to be applied to the template resolution.final LongReturns the TTL (Time To Live) in cache of templates resolved by this resolver.final TemplateModeReturns the template mode to be applied to templates resolved by this template resolver.final booleanReturns whether templates resolved by this resolver have to be considered cacheable or not.final voidsetCacheable(boolean cacheable) Sets a new value for the cacheable flag.final voidsetCacheTTLMs(Long cacheTTLMs) Sets a new value for the cache TTL for resolved templates.final voidsetTemplateMode(String templateMode) Sets the template mode to be applied to templates resolved by this resolver.final voidsetTemplateMode(TemplateMode templateMode) Sets the template mode to be applied to templates resolved by this resolver.voidsetUseDecoupledLogic(boolean useDecoupledLogic) Sets whether a separate (decoupled) resource containing template logic should be checked for existence and its instructions included into the resolved template during parsing.Methods inherited from class org.thymeleaf.templateresolver.AbstractTemplateResolver
computeResolvable, getCheckExistence, getName, getOrder, getResolvablePatterns, getResolvablePatternSpec, getUseDecoupledLogic, resolveTemplate, setCheckExistence, setName, setOrder, setResolvablePatterns
-
Field Details
-
DEFAULT_TEMPLATE_MODE
Default template mode:
TemplateMode.HTML -
DEFAULT_CACHEABLE
public static final boolean DEFAULT_CACHEABLEDefault value for the cacheable flag: false
- See Also:
-
DEFAULT_CACHE_TTL_MS
Default value for the cache TTL: null. This means the parsed template will live in cache until removed by LRU (because of being the oldest entry).
-
-
Constructor Details
-
StringTemplateResolver
public StringTemplateResolver()Creates a new instance of this template resolver.
-
-
Method Details
-
getTemplateMode
Returns the template mode to be applied to templates resolved by this template resolver.
- Returns:
- the template mode to be used.
-
setTemplateMode
Sets the template mode to be applied to templates resolved by this resolver.
- Parameters:
templateMode- the template mode.
-
setTemplateMode
Sets the template mode to be applied to templates resolved by this resolver.
Allowed templates modes are defined by the
TemplateModeclass.- Parameters:
templateMode- the template mode.
-
isCacheable
public final boolean isCacheable()Returns whether templates resolved by this resolver have to be considered cacheable or not.
- Returns:
- whether templates resolved are cacheable or not.
-
setCacheable
public final void setCacheable(boolean cacheable) Sets a new value for the cacheable flag.
- Parameters:
cacheable- whether resolved patterns should be considered cacheable or not.
-
getCacheTTLMs
Returns the TTL (Time To Live) in cache of templates resolved by this resolver.
If a template is resolved as cacheable but cache TTL is null, this means the template will live in cache until evicted by LRU (Least Recently Used) algorithm for being the oldest entry in cache.
- Returns:
- the cache TTL for resolved templates.
-
setCacheTTLMs
Sets a new value for the cache TTL for resolved templates.
If a template is resolved as cacheable but cache TTL is null, this means the template will live in cache until evicted by LRU (Least Recently Used) algorithm for being the oldest entry in cache.
- Parameters:
cacheTTLMs- the new cache TTL, or null for using natural LRU eviction.
-
setUseDecoupledLogic
public void setUseDecoupledLogic(boolean useDecoupledLogic) Description copied from class:AbstractTemplateResolverSets whether a separate (decoupled) resource containing template logic should be checked for existence and its instructions included into the resolved template during parsing.
This mechanism allows the creation of pure HTML or XML markup templates, which acquire their logic from an external resource. The way this decoupled resources are resolved is defined by a configured implementation of the
IDecoupledTemplateLogicResolverinterface.Note this flag can only be
truefor theTemplateMode.HTMLandTemplateMode.XMLtemplate modes. Also, note that setting this flag totruedoes not mean that a resource with decoupled logic must exist for the resolved template, only that it can exist and therefore it should be checked.Decoupled logic extracted from these additional resources is injected into the resolved templates in real-time as the resolved templates are parsed and processed. This greatly reduces overhead caused by decoupled parsing for non-cacheable templates, and completely removes any overhead for cached templates.
Default value is
FALSE.- Overrides:
setUseDecoupledLogicin classAbstractTemplateResolver- Parameters:
useDecoupledLogic-trueif resource existence should be checked,falseif not
-
computeTemplateResource
protected ITemplateResource computeTemplateResource(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String, Object> templateResolutionAttributes) Description copied from class:AbstractTemplateResolverComputes the resolved template resource.
- Specified by:
computeTemplateResourcein classAbstractTemplateResolver- Parameters:
configuration- the engine configuration.ownerTemplate- the owner template, if the resource being computed is a fragment. Might be null.template- the template to be resolved (usually its name).templateResolutionAttributes- the template resolution attributes, if any. Might be null.- Returns:
- the template resource, or null if this template cannot be resolved (or the resource does not exist).
-
computeTemplateMode
protected TemplateMode computeTemplateMode(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String, Object> templateResolutionAttributes) Description copied from class:AbstractTemplateResolverComputes the template mode that should be applied to a template, according to existing configuration.
- Specified by:
computeTemplateModein classAbstractTemplateResolver- Parameters:
configuration- the engine configuration.ownerTemplate- the owner template, if the resource being computed is a fragment. Might be null.template- the template to be resolved (usually its name).templateResolutionAttributes- the template resolution attributes, if any. Might be null.- Returns:
- the template mode proposed by the template resolver for the resolved template.
-
computeValidity
protected ICacheEntryValidity computeValidity(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String, Object> templateResolutionAttributes) Description copied from class:AbstractTemplateResolverComputes the validity to be applied to the template resolution. This includes determining whether the template can be cached or not, and also in what circumstances (for instance, for how much time) can its cache entry be considered valid.
- Specified by:
computeValidityin classAbstractTemplateResolver- Parameters:
configuration- the engine configuration.ownerTemplate- the owner template, if the resource being computed is a fragment. Might be null.template- the template to be resolved (usually its name).templateResolutionAttributes- the template resolution attributes, if any. Might be null.- Returns:
- the validity
-