Class StringTemplateResolver
- Object
-
- org.thymeleaf.templateresolver.AbstractTemplateResolver
-
- org.thymeleaf.templateresolver.StringTemplateResolver
-
- All Implemented Interfaces:
ITemplateResolver
public class StringTemplateResolver extends AbstractTemplateResolver
Implementation of
ITemplateResolver
that extendsAbstractTemplateResolver
and createsStringTemplateResource
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
TemplateMode1.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
Fields Modifier and Type Field Description static Long
DEFAULT_CACHE_TTL_MS
Default value for the cache TTL: null.static boolean
DEFAULT_CACHEABLE
Default value for the cacheable flag: falsestatic TemplateMode
DEFAULT_TEMPLATE_MODE
Default template mode:TemplateMode1.html
-
Fields inherited from class org.thymeleaf.templateresolver.AbstractTemplateResolver
DEFAULT_EXISTENCE_CHECK, DEFAULT_USE_DECOUPLED_LOGIC
-
-
Constructor Summary
Constructors Constructor Description StringTemplateResolver()
Creates a new instance of this template resolver.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected 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.Long
getCacheTTLMs()
Returns the TTL (Time To Live) in cache of templates resolved by this resolver.TemplateMode
getTemplateMode()
Returns the template mode to be applied to templates resolved by this template resolver.boolean
isCacheable()
Returns whether templates resolved by this resolver have to be considered cacheable or not.void
setCacheable(boolean cacheable)
Sets a new value for the cacheable flag.void
setCacheTTLMs(Long cacheTTLMs)
Sets a new value for the cache TTL for resolved templates.void
setTemplateMode(String templateMode)
Sets the template mode to be applied to templates resolved by this resolver.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 Detail
-
DEFAULT_TEMPLATE_MODE
public static final TemplateMode DEFAULT_TEMPLATE_MODE
Default template mode:
TemplateMode1.html
-
DEFAULT_CACHEABLE
public static final boolean DEFAULT_CACHEABLE
Default value for the cacheable flag: false
- See Also:
- Constant Field Values
-
DEFAULT_CACHE_TTL_MS
public static final Long 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).
-
-
Method Detail
-
getTemplateMode
public final TemplateMode getTemplateMode()
Returns the template mode to be applied to templates resolved by this template resolver.
- Returns:
- the template mode to be used.
-
setTemplateMode
public final void setTemplateMode(TemplateMode templateMode)
Sets the template mode to be applied to templates resolved by this resolver.
- Parameters:
templateMode
- the template mode.
-
setTemplateMode
public final void setTemplateMode(String templateMode)
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
public final Long 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
public final void setCacheTTLMs(Long cacheTTLMs)
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 theTemplateMode1.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
-
-