Class AbstractThymeleafView
- Object
-
- org.springframework.context.support.ApplicationObjectSupport
-
- org.springframework.web.context.support.WebApplicationObjectSupport
-
- org.thymeleaf.spring5.view.AbstractThymeleafView
-
- All Implemented Interfaces:
org.springframework.beans.factory.Aware
,org.springframework.beans.factory.BeanNameAware
,org.springframework.context.ApplicationContextAware
,org.springframework.web.context.ServletContextAware
,org.springframework.web.servlet.View
- Direct Known Subclasses:
ThymeleafView
public abstract class AbstractThymeleafView extends org.springframework.web.context.support.WebApplicationObjectSupport implements org.springframework.web.servlet.View, org.springframework.beans.factory.BeanNameAware
Abstract implementation class of the Spring MVC
View
interface for Thymeleaf.Views represent a template being executed, after being resolved (and instantiated) by a
ViewResolver
.- Since:
- 3.0.3
- Author:
- Daniel Fernández, Josh Long
-
-
Field Summary
Fields Modifier and Type Field Description static String
DEFAULT_CONTENT_TYPE
Default charset set to ISO-8859-1 for compatibility reasons with Spring's AbstractView.static boolean
DEFAULT_PRODUCE_PARTIAL_OUTPUT_WHILE_PROCESSING
By default Thymeleaf will not wait until a template is fully processed and rendered before starting to output its results.
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractThymeleafView()
Creates a new instance ofThymeleafView
.protected
AbstractThymeleafView(String templateName)
Creates a new instance ofThymeleafView
, specifying the template name.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected static void
addRequestContextAsVariable(Map<String,Object> model, String variableName, org.springframework.web.servlet.support.RequestContext requestContext)
void
addStaticVariable(String name, Object value)
Add a new static variable.String
getBeanName()
Returns the bean name.String
getCharacterEncoding()
Returns the character encoding set to be used for rendering this view.String
getContentType()
Returns the content type that will used for this view.boolean
getForceContentType()
Returns whether the configured content type should be forced instead of attempting a smart content type application based on template name.protected Locale
getLocale()
Returns the locale to be used for template processing.boolean
getProducePartialOutputWhileProcessing()
Returns whether Thymeleaf should start producing output –and sending it to the web server's output buffers– as soon as possible, outputting partial results while processing as they become available so that they can potentially be sent to the client (browser) before processing of the whole template has completely finished.Map<String,Object>
getStaticVariables()
Return the static variables, which will be available at the context every time this view is processed.protected ISpringTemplateEngine
getTemplateEngine()
Returns the template engine instance –aSpringTemplateEngine
instance, specifically– to be used for processing the template specified by this view object.String
getTemplateName()
Returns the name of the template being processed by this view object.protected boolean
isContentTypeSet()
protected boolean
isForceContentTypeSet()
protected boolean
isProducePartialOutputWhileProcessingSet()
void
setBeanName(String beanName)
Sets the bean name.void
setCharacterEncoding(String characterEncoding)
Specifies the character encoding to be set into the response when the view is rendered.void
setContentType(String contentType)
Sets the content type that will used for this view.void
setForceContentType(boolean forceContentType)
Sets whether the configured content type should be forced instead of attempting a smart content type application based on template name.protected void
setLocale(Locale locale)
Sets the locale to be used for template processing.void
setProducePartialOutputWhileProcessing(boolean producePartialOutputWhileProcessing)
Sets whether Thymeleaf should start producing output –and sending it to the web server's output buffers– as soon as possible, outputting partial results while processing as they become available so that they can potentially be sent to the client (browser) before processing of the whole template has completely finished.void
setStaticVariables(Map<String,?> variables)
Sets a set of static variables, which will be available at the context when this view is processed.protected void
setTemplateEngine(ISpringTemplateEngine templateEngine)
Sets the template engine instance –aSpringTemplateEngine
instance, specifically– to be used for processing the template specified by this view object.void
setTemplateName(String templateName)
Sets the name of the template to be processed by this view object.-
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
-
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext
-
-
-
-
Field Detail
-
DEFAULT_CONTENT_TYPE
public static final String DEFAULT_CONTENT_TYPE
Default charset set to ISO-8859-1 for compatibility reasons with Spring's AbstractView. Value is
"text/html;charset=ISO-8859-1"
.- See Also:
- Constant Field Values
-
DEFAULT_PRODUCE_PARTIAL_OUTPUT_WHILE_PROCESSING
public static final boolean DEFAULT_PRODUCE_PARTIAL_OUTPUT_WHILE_PROCESSING
By default Thymeleaf will not wait until a template is fully processed and rendered before starting to output its results. Instead, it will start producing output as soon as possible while the template is still being processed. Value is
true
.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
AbstractThymeleafView
protected AbstractThymeleafView()
Creates a new instance of
ThymeleafView
.
-
AbstractThymeleafView
protected AbstractThymeleafView(String templateName)
Creates a new instance of
ThymeleafView
, specifying the template name.- Parameters:
templateName
- the template name.
-
-
Method Detail
-
getContentType
public String getContentType()
Returns the content type that will used for this view.
Content type will be computed this way:
- If a value is specified calling
setContentType(String)
on this object, that value will be used. - If a value is specified at the view resolver by calling
ThymeleafViewResolver.setContentType(String)
, that one will be used. - If none of the above is true, the
DEFAULT_CONTENT_TYPE
constant with value "text/html;charset=ISO-8859-1" will be used.
- Specified by:
getContentType
in interfaceorg.springframework.web.servlet.View
- Returns:
- the content type
- See Also:
ThymeleafViewResolver.getContentType()
- If a value is specified calling
-
setContentType
public void setContentType(String contentType)
Sets the content type that will used for this view.
Content type will be computed this way:
- If a value is specified calling this method, that value will be used.
- If a value is specified at the view resolver by calling
ThymeleafViewResolver.setContentType(String)
, that one will be used. - If none of the above is true, the
DEFAULT_CONTENT_TYPE
constant with value "text/html;charset=ISO-8859-1" will be used.
- Parameters:
contentType
- the content type to be used.- See Also:
ThymeleafViewResolver.setContentType(String)
-
isContentTypeSet
protected boolean isContentTypeSet()
-
getForceContentType
public boolean getForceContentType()
Returns whether the configured content type should be forced instead of attempting a smart content type application based on template name.
When forced, the configured content type (
setForceContentType(boolean)
) will be applied even if the template name ends in a known suffix:.html
,.htm
,.xhtml
,.xml
,.js
,.json
,.css
,.rss
,.atom
,.txt
.Default value is
.false
- Returns:
- whether the content type will be forced or not.
- Since:
- 3.0.6
-
setForceContentType
public void setForceContentType(boolean forceContentType)
Sets whether the configured content type should be forced instead of attempting a smart content type application based on template name.
When forced, the configured content type (
setForceContentType(boolean)
) will be applied even if the template name ends in a known suffix:.html
,.htm
,.xhtml
,.xml
,.js
,.json
,.css
,.rss
,.atom
,.txt
.Default value is
.false
- Parameters:
forceContentType
- whether the configured template mode should be forced or not.- Since:
- 3.0.6
-
isForceContentTypeSet
protected boolean isForceContentTypeSet()
-
getCharacterEncoding
public String getCharacterEncoding()
Returns the character encoding set to be used for rendering this view.
Many times, character encoding is specified as a part of the content type using the
setContentType(String)
method, but this is not mandatory, and it could be that only the MIME type is specified that way, thus allowing to set the character encoding using thesetCharacterEncoding(String)
counterpart of this getter method.- Returns:
- the character encoding.
-
setCharacterEncoding
public void setCharacterEncoding(String characterEncoding)
Specifies the character encoding to be set into the response when the view is rendered.
Many times, character encoding is specified as a part of the content type using the
setContentType(String)
method, but this is not mandatory, and it could be that only the MIME type is specified that way, thus allowing to set the character encoding using this method.- Parameters:
characterEncoding
- the character encoding to be used (e.g.UTF-8
,ISO-8859-1
, etc.)
-
getProducePartialOutputWhileProcessing
public boolean getProducePartialOutputWhileProcessing()
Returns whether Thymeleaf should start producing output –and sending it to the web server's output buffers– as soon as possible, outputting partial results while processing as they become available so that they can potentially be sent to the client (browser) before processing of the whole template has completely finished.
If set to
false
, no fragments of template result will be sent to the web server's output buffers until Thymeleaf completely finishes processing the template and generating the corresponding output. Only once finished will output start to be written to the web server's output buffers, and therefore sent to the clients.Note that setting this to
false
is not recommended for most scenarios, as it can (very) significantly increase the amount of memory used per template execution. Only modify this setting if you know what you are doing. A typical scenario in which setting this tofalse
could be of use is when an application is suffering from UI rendering issues (flickering) at the browser due to incremental rendering of very large templates.Default value is
true
.- Returns:
- whether to start producing output as soon as possible while processing or not (default:
true
). - Since:
- 3.0.10
-
setProducePartialOutputWhileProcessing
public void setProducePartialOutputWhileProcessing(boolean producePartialOutputWhileProcessing)
Sets whether Thymeleaf should start producing output –and sending it to the web server's output buffers– as soon as possible, outputting partial results while processing as they become available so that they can potentially be sent to the client (browser) before processing of the whole template has completely finished.
If set to
false
, no fragments of template result will be sent to the web server's output buffers until Thymeleaf completely finishes processing the template and generating the corresponding output. Only once finished will output start to be written to the web server's output buffers, and therefore sent to the clients.Note that setting this to
false
is not recommended for most scenarios, as it can (very) significantly increase the amount of memory used per template execution. Only modify this setting if you know what you are doing. A typical scenario in which setting this tofalse
could be of use is when an application is suffering from UI rendering issues (flickering) at the browser due to incremental rendering of very large templates.Default value is
true
.- Parameters:
producePartialOutputWhileProcessing
- whether to start producing output as soon as possible while processing or not (default:true
).- Since:
- 3.0.10
-
isProducePartialOutputWhileProcessingSet
protected boolean isProducePartialOutputWhileProcessingSet()
-
getBeanName
public String getBeanName()
Returns the bean name.
- Returns:
- the bean name.
-
setBeanName
public void setBeanName(String beanName)
Sets the bean name.
- Specified by:
setBeanName
in interfaceorg.springframework.beans.factory.BeanNameAware
- Parameters:
beanName
- the new bean name.
-
getTemplateName
public String getTemplateName()
Returns the name of the template being processed by this view object.
This name will be specified in the same shape it will be resolved by the template resolvers (i.e. as it is returned by controllers, without any prefixes/suffixes).
- Returns:
- the template name.
-
setTemplateName
public void setTemplateName(String templateName)
Sets the name of the template to be processed by this view object.
This name will be specified in the same shape it will be resolved by the template resolvers (i.e. as it is returned by controllers, without any prefixes/suffixes).
- Parameters:
templateName
- the template name
-
getLocale
protected Locale getLocale()
Returns the locale to be used for template processing.
- Returns:
- the locale
-
setLocale
protected void setLocale(Locale locale)
Sets the locale to be used for template processing. Usually, the View Resolver will set this automatically from user session / application data.
- Parameters:
locale
- the locale to be used.
-
getTemplateEngine
protected ISpringTemplateEngine getTemplateEngine()
Returns the template engine instance –a
SpringTemplateEngine
instance, specifically– to be used for processing the template specified by this view object.- Returns:
- the template engine instance
-
setTemplateEngine
protected void setTemplateEngine(ISpringTemplateEngine templateEngine)
Sets the template engine instance –a
SpringTemplateEngine
instance, specifically– to be used for processing the template specified by this view object.- Parameters:
templateEngine
- the template engine instance to be used
-
getStaticVariables
public Map<String,Object> getStaticVariables()
Return the static variables, which will be available at the context every time this view is processed.
These static variables are added to the context before the view is processed, so that they can be referenced from the context like any other context variables, for example:
${myStaticVar}
.- Returns:
- the map of static variables to be set into view's execution.
-
addStaticVariable
public void addStaticVariable(String name, Object value)
Add a new static variable.
These static variables are added to the context before this view is processed, so that they can be referenced from the context like any other context variables, for example:
${myStaticVar}
.- Parameters:
name
- the name of the static variablevalue
- the value of the static variable
-
setStaticVariables
public void setStaticVariables(Map<String,?> variables)
Sets a set of static variables, which will be available at the context when this view is processed.
This method does not overwrite the existing static variables, it simply adds the ones specify to any variables already registered.
These static variables are added to the context before this view is processed, so that they can be referenced from the context like any other context variables, for example:
${myStaticVar}
.- Parameters:
variables
- the set of variables to be added.
-
-