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
Modifier and TypeFieldDescriptionstatic final Long
Default value for the cache TTL: null.static final boolean
Default value for the cacheable flag: falsestatic final TemplateMode
Default template mode:TemplateMode.HTML
Fields inherited from class org.thymeleaf.templateresolver.AbstractTemplateResolver
DEFAULT_EXISTENCE_CHECK, DEFAULT_USE_DECOUPLED_LOGIC
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected TemplateMode
computeTemplateMode
(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 ITemplateResource
computeTemplateResource
(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String, Object> templateResolutionAttributes) Computes the resolved template resource.protected ICacheEntryValidity
computeValidity
(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String, Object> templateResolutionAttributes) Computes the validity to be applied to the template resolution.final Long
Returns the TTL (Time To Live) in cache of templates resolved by this resolver.final TemplateMode
Returns the template mode to be applied to templates resolved by this template resolver.final boolean
Returns whether templates resolved by this resolver have to be considered cacheable or not.final void
setCacheable
(boolean cacheable) Sets a new value for the cacheable flag.final void
setCacheTTLMs
(Long cacheTTLMs) Sets a new value for the cache TTL for resolved templates.final void
setTemplateMode
(String templateMode) Sets the template mode to be applied to templates resolved by this resolver.final void
setTemplateMode
(TemplateMode templateMode) Sets the template mode to be applied to templates resolved by this resolver.void
setUseDecoupledLogic
(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
TemplateMode
class.- 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:AbstractTemplateResolver
Sets 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
IDecoupledTemplateLogicResolver
interface.Note this flag can only be
true
for theTemplateMode.HTML
andTemplateMode.XML
template modes. Also, note that setting this flag totrue
does 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:
setUseDecoupledLogic
in classAbstractTemplateResolver
- Parameters:
useDecoupledLogic
-true
if resource existence should be checked,false
if not
-
computeTemplateResource
protected ITemplateResource computeTemplateResource(IEngineConfiguration configuration, String ownerTemplate, String template, Map<String, Object> templateResolutionAttributes) Description copied from class:AbstractTemplateResolver
Computes the resolved template resource.
- Specified by:
computeTemplateResource
in 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:AbstractTemplateResolver
Computes the template mode that should be applied to a template, according to existing configuration.
- Specified by:
computeTemplateMode
in 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:AbstractTemplateResolver
Computes 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:
computeValidity
in 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
-