Class WebEngineContext
- java.lang.Object
-
- org.thymeleaf.context.AbstractEngineContext
-
- org.thymeleaf.context.WebEngineContext
-
- All Implemented Interfaces:
IContext,IEngineContext,IExpressionContext,ITemplateContext,IWebContext
public class WebEngineContext extends AbstractEngineContext implements IEngineContext, IWebContext
Basic web implementation of the
IEngineContextinterface, with added web-oriented capabilities.This is the context implementation that will be used by default for web processing. Note that this is an internal implementation, and there is no reason for users' code to directly reference or use it instead of its implemented interfaces.
This class is NOT thread-safe. Thread-safety is not a requirement for context implementations.
Note this class was modified in a backwards-incompatible way in Thymeleaf 3.1.0.
- Since:
- 3.1.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classWebEngineContext.ApplicationAttributeMapprivate static classWebEngineContext.ExchangeAttributeMapprivate static classWebEngineContext.NoOpMapImplprivate static classWebEngineContext.RequestParameterMapstatic classWebEngineContext.RequestParameterValuesprivate static classWebEngineContext.SessionAttributeMap
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringAPPLICATION_VARIABLE_NAMEprivate WebEngineContext.ApplicationAttributeMapapplicationAttributeMapprivate WebEngineContext.ExchangeAttributeMapexchangeAttributeMapprivate static java.lang.StringPARAM_VARIABLE_NAMEprivate WebEngineContext.RequestParameterMaprequestParameterMapprivate static java.lang.StringSESSION_VARIABLE_NAMEprivate WebEngineContext.SessionAttributeMapsessionAttributeMapprivate IWebExchangewebExchange
-
Constructor Summary
Constructors Constructor Description WebEngineContext(IEngineConfiguration configuration, TemplateData templateData, java.util.Map<java.lang.String,java.lang.Object> templateResolutionAttributes, IWebExchange webExchange, java.util.Locale locale, java.util.Map<java.lang.String,java.lang.Object> variables)Creates a new instance of thisIEngineContextimplementation binding engine execution to the Servlet API.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancontainsVariable(java.lang.String name)Checks whether a specific variable is already contained in this context or not.voiddecreaseLevel()Decrease the context level.java.util.List<IProcessableElementTag>getElementStack()Returns the list of all theIProcessableElementTagobjects corresponding to the hierarchy of elements (open or standalone elements) that had to be processed in order to reach the current point in execution.java.util.List<IProcessableElementTag>getElementStackAbove(int contextLevel)Retrieves the element stack just likeITemplateContext.getElementStack(), but only for those elements added to the hierarchy above a specific context level.IWebExchangegetExchange()Returns theIWebExchangeobject associated with the template execution.IInlinergetInliner()Returns the inliner (implementation ofIInliner) set to be used at the current point of execution.java.lang.ObjectgetSelectionTarget()Returns the selection target set for the current point of execution (ornullif there isn't any).java.lang.StringgetStringRepresentationByLevel()TemplateDatagetTemplateData()Returns the template data object containing metadata about the template currently being processed.java.util.List<TemplateData>getTemplateStack()Returns the list of all theTemplateDataobjects corresponding to all the templates that have been nested in order to reach the current execution point.java.lang.ObjectgetVariable(java.lang.String key)Retrieve a specific variable, by name.java.util.Set<java.lang.String>getVariableNames()Get a list with all the names of variables contained at this context.booleanhasSelectionTarget()Returns whether the current template has set a selection target for the current point of execution or not.voidincreaseLevel()Increase the context level.booleanisVariableLocal(java.lang.String name)Checks whether a specific variable is local or not.intlevel()Return the current context level.voidremoveVariable(java.lang.String name)Removes a variable from the context.(package private) static java.lang.ObjectresolveLazy(java.lang.Object variable)voidsetElementTag(IProcessableElementTag elementTag)Sets a new element tag (IProcessableElementTag) into the hierarchy (stack) of element tags.voidsetInliner(IInliner inliner)Set an inliner.voidsetSelectionTarget(java.lang.Object selectionTarget)Set a selection target.voidsetTemplateData(TemplateData templateData)Sets a new template metadata object (TemplateData) for the current execution point, specifying that the elements and nodes that are to be processed from now on (until the context level is decreased below the current level) originally belonged to a different template.voidsetVariable(java.lang.String name, java.lang.Object value)Sets a new variable into the context.voidsetVariables(java.util.Map<java.lang.String,java.lang.Object> variables)Sets several variables at a time into the context.java.lang.StringtoString()-
Methods inherited from class org.thymeleaf.context.AbstractEngineContext
buildLink, getConfiguration, getExpressionObjects, getIdentifierSequences, getLocale, getMessage, getModelFactory, getTemplateMode, getTemplateResolutionAttributes
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.thymeleaf.context.IExpressionContext
getConfiguration, getExpressionObjects
-
Methods inherited from interface org.thymeleaf.context.ITemplateContext
buildLink, getIdentifierSequences, getMessage, getModelFactory, getTemplateMode, getTemplateResolutionAttributes
-
-
-
-
Field Detail
-
PARAM_VARIABLE_NAME
private static final java.lang.String PARAM_VARIABLE_NAME
- See Also:
- Constant Field Values
-
SESSION_VARIABLE_NAME
private static final java.lang.String SESSION_VARIABLE_NAME
- See Also:
- Constant Field Values
-
APPLICATION_VARIABLE_NAME
private static final java.lang.String APPLICATION_VARIABLE_NAME
- See Also:
- Constant Field Values
-
webExchange
private final IWebExchange webExchange
-
exchangeAttributeMap
private final WebEngineContext.ExchangeAttributeMap exchangeAttributeMap
-
requestParameterMap
private final WebEngineContext.RequestParameterMap requestParameterMap
-
sessionAttributeMap
private final WebEngineContext.SessionAttributeMap sessionAttributeMap
-
applicationAttributeMap
private final WebEngineContext.ApplicationAttributeMap applicationAttributeMap
-
-
Constructor Detail
-
WebEngineContext
public WebEngineContext(IEngineConfiguration configuration, TemplateData templateData, java.util.Map<java.lang.String,java.lang.Object> templateResolutionAttributes, IWebExchange webExchange, java.util.Locale locale, java.util.Map<java.lang.String,java.lang.Object> variables)
Creates a new instance of this
IEngineContextimplementation binding engine execution to the Servlet API.Note that implementations of
IEngineContextare not meant to be used in order to call the template engine (use implementations ofIContextsuch asContextorWebContextinstead). This is therefore mostly an internal implementation, and users should have no reason to ever call this constructor except in very specific integration/extension scenarios.- Parameters:
configuration- the configuration instance being used.templateData- the template data for the template to be processed.templateResolutionAttributes- the template resolution attributes.webExchange- the web exchange object.locale- the locale.variables- the context variables, probably coming from anotherIContextimplementation.
-
-
Method Detail
-
getExchange
public IWebExchange getExchange()
Description copied from interface:IWebContextReturns the
IWebExchangeobject associated with the template execution.- Specified by:
getExchangein interfaceIWebContext- Returns:
- the web exchange object.
-
containsVariable
public boolean containsVariable(java.lang.String name)
Description copied from interface:IContextChecks whether a specific variable is already contained in this context or not.
- Specified by:
containsVariablein interfaceIContext- Parameters:
name- the name of the variable to be checked.- Returns:
trueif the variable is already contained,falseif not.
-
getVariable
public java.lang.Object getVariable(java.lang.String key)
Description copied from interface:IContextRetrieve a specific variable, by name.
- Specified by:
getVariablein interfaceIContext- Parameters:
key- the name of the variable to be retrieved.- Returns:
- the variable's value.
-
getVariableNames
public java.util.Set<java.lang.String> getVariableNames()
Description copied from interface:IContextGet a list with all the names of variables contained at this context.
- Specified by:
getVariableNamesin interfaceIContext- Returns:
- the variable names.
-
setVariable
public void setVariable(java.lang.String name, java.lang.Object value)Description copied from interface:IEngineContextSets a new variable into the context.
Depending on the context level, determined by
IEngineContext.increaseLevel()andIEngineContext.decreaseLevel(), the variable being set might be considered a local variable and thus disappear from context once the context level is decreased below the level the variable was created at.- Specified by:
setVariablein interfaceIEngineContext- Parameters:
name- the name of the variable.value- the value of the variable.
-
setVariables
public void setVariables(java.util.Map<java.lang.String,java.lang.Object> variables)
Description copied from interface:IEngineContextSets several variables at a time into the context.
Depending on the context level, determined by
IEngineContext.increaseLevel()andIEngineContext.decreaseLevel(), the variables being set might be considered a local variables and thus disappear from context once the context level is decreased below the level the variable was created at.- Specified by:
setVariablesin interfaceIEngineContext- Parameters:
variables- the variables to be set.
-
removeVariable
public void removeVariable(java.lang.String name)
Description copied from interface:IEngineContextRemoves a variable from the context.
Depending on the context level, determined by
IEngineContext.increaseLevel()andIEngineContext.decreaseLevel(), this removal might be considered local variable-related and thus cease to happen (i.e. the variable would be recovered) once the context level is decreased below the level the variable was created at.- Specified by:
removeVariablein interfaceIEngineContext- Parameters:
name- the name of the variable to be removed.
-
isVariableLocal
public boolean isVariableLocal(java.lang.String name)
Description copied from interface:IEngineContextChecks whether a specific variable is local or not.
This means checking if the context level at which the variable was defined was 0 or not.
- Specified by:
isVariableLocalin interfaceIEngineContext- Parameters:
name- the name of the variable to be checked.- Returns:
trueif the variable is local (level > 0),falseif not (level == 0).
-
hasSelectionTarget
public boolean hasSelectionTarget()
Description copied from interface:ITemplateContextReturns whether the current template has set a selection target for the current point of execution or not.
Selection targets are objects on which all
*{...}expression will be executed (instead of on the root context). They are normally set by means ofth:objects.- Specified by:
hasSelectionTargetin interfaceITemplateContext- Returns:
trueif there is a selection target,falseif not.
-
getSelectionTarget
public java.lang.Object getSelectionTarget()
Description copied from interface:ITemplateContextReturns the selection target set for the current point of execution (or
nullif there isn't any).Selection targets are objects on which all
*{...}expression will be executed (instead of on the root context). They are normally set by means ofth:objects.- Specified by:
getSelectionTargetin interfaceITemplateContext- Returns:
- the selection target, or null if there isn't any.
-
setSelectionTarget
public void setSelectionTarget(java.lang.Object selectionTarget)
Description copied from interface:IEngineContextSet a selection target. Usually the consequence of executing a
th:objectprocessor.Once set, all selection expressions (
*{...}) will be executed on this target.This selection target will have the consideration of a local variable and thus depend on the context level (see
IEngineContext.setVariable(String, Object)).- Specified by:
setSelectionTargetin interfaceIEngineContext- Parameters:
selectionTarget- the selection target to be set.
-
getInliner
public IInliner getInliner()
Description copied from interface:ITemplateContextReturns the inliner (implementation of
IInliner) set to be used at the current point of execution.- Specified by:
getInlinerin interfaceITemplateContext- Returns:
- the inliner to be used.
-
setInliner
public void setInliner(IInliner inliner)
Description copied from interface:IEngineContextSet an inliner. Usually the consequence of executing a
th:inlineprocessor.This inliner will have the consideration of a local variable and thus depend on the context level (see
IEngineContext.setVariable(String, Object)).- Specified by:
setInlinerin interfaceIEngineContext- Parameters:
inliner- the inliner to be set.
-
getTemplateData
public TemplateData getTemplateData()
Description copied from interface:ITemplateContextReturns the template data object containing metadata about the template currently being processed.
Note that the
TemplateDatareturned here corresponds with the origin of the elements or nodes being currently processed. This is, if a processor is being executed for an element inserted from an external template (via ath:insert, for example), then this method will return the template data for the template in which the inserted fragment lives, not the one it was inserted into.- Specified by:
getTemplateDatain interfaceITemplateContext- Returns:
- the template data corresponding to the elements or nodes being currently processed.
-
setTemplateData
public void setTemplateData(TemplateData templateData)
Description copied from interface:IEngineContextSets a new template metadata object (
TemplateData) for the current execution point, specifying that the elements and nodes that are to be processed from now on (until the context level is decreased below the current level) originally belonged to a different template.A call on this method is usually the consequence of
th:insertorth:replace.- Specified by:
setTemplateDatain interfaceIEngineContext- Parameters:
templateData- the template data.
-
getTemplateStack
public java.util.List<TemplateData> getTemplateStack()
Description copied from interface:ITemplateContextReturns the list of all the
TemplateDataobjects corresponding to all the templates that have been nested in order to reach the current execution point.This is a way in which processors can know the complete execution route that lead to the execution of a specific event (e.g. Template A inserted fragment B, which inserted fragment C).
The first element in this list will always be the top-level template (the one called at the
ITemplateEngineprocess(...)methods).- Specified by:
getTemplateStackin interfaceITemplateContext- Returns:
- the stack of templates (list of
TemplateData).
-
setElementTag
public void setElementTag(IProcessableElementTag elementTag)
Description copied from interface:IEngineContextSets a new element tag (
IProcessableElementTag) into the hierarchy (stack) of element tags.This hierarchy of element tags (added this way) can be obtained with
ITemplateContext.getElementStack().- Specified by:
setElementTagin interfaceIEngineContext- Parameters:
elementTag- the element tag.
-
getElementStack
public java.util.List<IProcessableElementTag> getElementStack()
Description copied from interface:ITemplateContextReturns the list of all the
IProcessableElementTagobjects corresponding to the hierarchy of elements (open or standalone elements) that had to be processed in order to reach the current point in execution.If the element being processed is a tag (open/standalone), it will appear at the end of the list.
Note this hierarchy does not correspond with the tag hierarchy at the original template, but with the hierarchy of processing (many tags could appear during processing itself and not be present at the original template).
Also note that, because of this being the processing-time hierarchy, this information is set at the processor level, so it should not be considered to be available and/or valid at the pre-processor layer.
- Specified by:
getElementStackin interfaceITemplateContext- Returns:
- the stack of elements (list of
IProcessableElementTag).
-
getElementStackAbove
public java.util.List<IProcessableElementTag> getElementStackAbove(int contextLevel)
Description copied from interface:IEngineContextRetrieves the element stack just like
ITemplateContext.getElementStack(), but only for those elements added to the hierarchy above a specific context level.- Specified by:
getElementStackAbovein interfaceIEngineContext- Parameters:
contextLevel- the level above which we want to obtain the element stack.- Returns:
- the element stack above a specified level.
-
level
public int level()
Description copied from interface:IEngineContextReturn the current context level.
This method should only be called internally.
- Specified by:
levelin interfaceIEngineContext- Returns:
- the current level
-
increaseLevel
public void increaseLevel()
Description copied from interface:IEngineContextIncrease the context level. This is usually a consequence of the
ProcessorTemplateHandlerdetecting the start of a new element (i.e. handling anIOpenElementTagevent).This method should only be called internally.
- Specified by:
increaseLevelin interfaceIEngineContext
-
decreaseLevel
public void decreaseLevel()
Description copied from interface:IEngineContextDecrease the context level. This is usually a consequence of the
ProcessorTemplateHandlerdetecting the closing of an element (i.e. handling anICloseElementTagevent).This method should only be called internally.
- Specified by:
decreaseLevelin interfaceIEngineContext
-
getStringRepresentationByLevel
public java.lang.String getStringRepresentationByLevel()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
resolveLazy
static java.lang.Object resolveLazy(java.lang.Object variable)
-
-