public class ThymeleafViewResolver
extends org.springframework.web.servlet.view.AbstractCachingViewResolver
implements org.springframework.core.Ordered
Implementation of the Spring MVC ViewResolver
interface.
View resolvers execute after the controller ends its execution. They receive the name
of the view to be processed and are in charge of creating (and configuring) the
corresponding View
object for it.
The View
implementations managed by this class are subclasses of
AbstractThymeleafView
. By default, ThymeleafView
is used.
Modifier and Type | Field and Description |
---|---|
static String |
FORWARD_URL_PREFIX
Prefix to be used in view names (returned by controllers) for specifying an
HTTP forward.
|
static String |
REDIRECT_URL_PREFIX
Prefix to be used in view names (returned by controllers) for specifying an
HTTP redirect.
|
Constructor and Description |
---|
ThymeleafViewResolver()
Create an instance of ThymeleafViewResolver.
|
Modifier and Type | Method and Description |
---|---|
void |
addStaticVariable(String name,
Object value)
Add a new static variable.
|
protected boolean |
canHandle(String viewName,
Locale locale) |
protected org.springframework.web.servlet.View |
createView(String viewName,
Locale locale) |
String |
getCharacterEncoding()
Returns the character encoding set to be used for all views resolved by
this view resolver.
|
String |
getContentType()
Returns the content type that will be set into views resolved by this
view resolver.
|
String[] |
getExcludedViewNames()
Returns the names of views –patterns, in fact– that cannot
be handled by this view resolver.
|
int |
getOrder()
Returns the order in which this view resolver will be queried.
|
Map<String,Object> |
getStaticVariables()
Return the static variables, which will be available at the context
every time a view resolved by this ViewResolver is processed.
|
SpringTemplateEngine |
getTemplateEngine()
Returns the Thymeleaf template engine instance to be used for the
execution of templates.
|
protected Class<? extends AbstractThymeleafView> |
getViewClass()
Return the view class to be used to create views.
|
String[] |
getViewNames()
Return the set of name patterns that will applied to determine whether a view name
returned by a controller will be resolved by this resolver or not.
|
boolean |
isRedirectContextRelative()
Return whether to interpret a given redirect URL that starts with a slash ("/")
as relative to the current ServletContext, i.e.
|
boolean |
isRedirectHttp10Compatible()
Return whether redirects should stay compatible with HTTP 1.0 clients.
|
protected org.springframework.web.servlet.View |
loadView(String viewName,
Locale locale) |
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 to be used when rendering views.
|
void |
setExcludedViewNames(String[] excludedViewNames)
Specify names of views –patterns, in fact– that cannot
be handled by this view resolver.
|
void |
setOrder(int order)
Specify the order in which this view resolver will be queried.
|
void |
setRedirectContextRelative(boolean redirectContextRelative)
Set whether to interpret a given redirect URL that starts with a slash ("/")
as relative to the current ServletContext, i.e.
|
void |
setRedirectHttp10Compatible(boolean redirectHttp10Compatible)
Set whether redirects should stay compatible with HTTP 1.0 clients.
|
void |
setStaticVariables(Map<String,?> variables)
Sets a set of static variables, which will be available at the context
every time a view resolved by this ViewResolver is processed.
|
void |
setTemplateEngine(SpringTemplateEngine templateEngine)
Sets the Template Engine instance to be used for processing
templates.
|
void |
setViewClass(Class<? extends AbstractThymeleafView> viewClass)
Set the view class that should be used to create views.
|
void |
setViewNames(String[] viewNames)
Specify a set of name patterns that will applied to determine whether a view name
returned by a controller will be resolved by this resolver or not.
|
clearCache, getCacheKey, isCache, isCacheUnresolved, removeFromCache, resolveViewName, setCache, setCacheUnresolved
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
public static final String REDIRECT_URL_PREFIX
Prefix to be used in view names (returned by controllers) for specifying an HTTP redirect.
Value: redirect:
public static final String FORWARD_URL_PREFIX
Prefix to be used in view names (returned by controllers) for specifying an HTTP forward.
Value: forward:
public ThymeleafViewResolver()
Create an instance of ThymeleafViewResolver.
public void setViewClass(Class<? extends AbstractThymeleafView> viewClass)
Set the view class that should be used to create views. This must be a subclass
of AbstractThymeleafView
. The default value is ThymeleafView
.
viewClass
- class that is assignable to the required view class
(by default, ThmeleafView).protected Class<? extends AbstractThymeleafView> getViewClass()
Return the view class to be used to create views.
public SpringTemplateEngine getTemplateEngine()
Returns the Thymeleaf template engine instance to be used for the execution of templates.
Template engine instances to be used for this view resolver should be of a
subclass of TemplateEngine
including
specific Spring integrations: SpringTemplateEngine
.
public void setTemplateEngine(SpringTemplateEngine templateEngine)
Sets the Template Engine instance to be used for processing templates.
Template engine instances to be used for this view resolver should be of a
subclass of TemplateEngine
including
specific Spring integrations: SpringTemplateEngine
.
templateEngine
- the template engine to be usedpublic Map<String,Object> getStaticVariables()
Return the static variables, which will be available at the context every time a view resolved by this ViewResolver is processed.
These static variables are added to the context by the view resolver before every view is processed, so that they can be referenced from the context like any other context variables, for example: ${myStaticVar}.
public void addStaticVariable(String name, Object value)
Add a new static variable.
These static variables are added to the context by the view resolver before every view is processed, so that they can be referenced from the context like any other context variables, for example: ${myStaticVar}.
name
- the name of the static variablevalue
- the value of the static variablepublic void setStaticVariables(Map<String,?> variables)
Sets a set of static variables, which will be available at the context every time a view resolved by this ViewResolver 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 by the view resolver before every view is processed, so that they can be referenced from the context like any other context variables, for example: ${myStaticVar}.
variables
- the set of variables to be added.public void setOrder(int order)
Specify the order in which this view resolver will be queried.
Spring MVC applications can have several view resolvers configured, and this order property established the order in which they will be queried for view resolution.
order
- the order in which this view resolver will be asked to resolve
the view.public int getOrder()
Returns the order in which this view resolver will be queried.
Spring MVC applications can have several view resolvers configured, and this order property established the order in which they will be queried for view resolution.
getOrder
in interface org.springframework.core.Ordered
public void setContentType(String contentType)
Sets the content type to be used when rendering views.
This content type acts as a default, so that every view resolved by this resolver will use this content type unless there is a bean defined for such view that specifies a different content type.
Therefore, individual views are allowed to specify their own content type regardless of the application-wide setting established here.
If a content type is not specified (either here or at a specific view definition),
AbstractThymeleafView.DEFAULT_CONTENT_TYPE
will be used.
contentType
- the content type to be used.public String getContentType()
Returns the content type that will be set into views resolved by this view resolver.
This content type acts as a default, so that every view resolved by this resolver will use this content type unless there is a bean defined for such view that specifies a different content type.
Therefore, individual views are allowed to specify their own content type regardless of the application-wide setting established here.
If a content type is not specified (either at the view resolver or at a specific
view definition), AbstractThymeleafView.DEFAULT_CONTENT_TYPE
will be used.
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)
or
AbstractThymeleafView.setContentType(String)
, 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.
As with setContentType(String)
, the value specified here acts as a
default in case no character encoding has been specified at the view itself.
If a view bean exists with the name of the view to be processed, and this
view has been set a value for its AbstractThymeleafView.setCharacterEncoding(String)
method, the value specified at the view resolver has no effect.
characterEncoding
- the character encoding to be used (e.g. UTF-8,
ISO-8859-1, etc.)public String getCharacterEncoding()
Returns the character encoding set to be used for all views resolved by this view resolver.
Many times, character encoding is specified as a part of the content
type, using the setContentType(String)
or
AbstractThymeleafView.setContentType(String)
, 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 the setCharacterEncoding(String)
counterpart of this getter method.
As with setContentType(String)
, the value specified here acts as a
default in case no character encoding has been specified at the view itself.
If a view bean exists with the name of the view to be processed, and this
view has been set a value for its AbstractThymeleafView.setCharacterEncoding(String)
method, the value specified at the view resolver has no effect.
public void setRedirectContextRelative(boolean redirectContextRelative)
Set whether to interpret a given redirect URL that starts with a slash ("/") as relative to the current ServletContext, i.e. as relative to the web application root.
Default is true: A redirect URL that starts with a slash will be interpreted as relative to the web application root, i.e. the context path will be prepended to the URL.
Redirect URLs can be specified via the "redirect:" prefix. e.g.: "redirect:myAction.do".
redirectContextRelative
- whether redirect URLs should be considered context-relative or not.RedirectView.setContextRelative(boolean)
public boolean isRedirectContextRelative()
Return whether to interpret a given redirect URL that starts with a slash ("/") as relative to the current ServletContext, i.e. as relative to the web application root.
Default is true.
RedirectView.setContextRelative(boolean)
public void setRedirectHttp10Compatible(boolean redirectHttp10Compatible)
Set whether redirects should stay compatible with HTTP 1.0 clients.
In the default implementation (default is true), this will enforce HTTP status
code 302 in any case, i.e. delegate to
HttpServletResponse.sendRedirect(String)
. Turning this off
will send HTTP status code 303, which is the correct code for HTTP 1.1 clients, but not understood
by HTTP 1.0 clients.
Many HTTP 1.1 clients treat 302 just like 303, not making any difference. However, some clients depend on 303 when redirecting after a POST request; turn this flag off in such a scenario.
Redirect URLs can be specified via the "redirect:" prefix. e.g.: "redirect:myAction.do"
redirectHttp10Compatible
- true if redirects should stay compatible with HTTP 1.0 clients,
false if not.RedirectView.setHttp10Compatible(boolean)
public boolean isRedirectHttp10Compatible()
Return whether redirects should stay compatible with HTTP 1.0 clients.
Default is true.
RedirectView.setHttp10Compatible(boolean)
public void setViewNames(String[] viewNames)
Specify a set of name patterns that will applied to determine whether a view name returned by a controller will be resolved by this resolver or not.
In applications configuring several view resolvers –for example, one for Thymeleaf and another one for JSP+JSTL legacy pages–, this property establishes when a view will be considered to be resolved by this view resolver and when Spring should simply ask the next resolver in the chain –according to its order– instead.
The specified view name patterns can be complete view names, but can also use the * wildcard: "index.*", "user_*", "admin/*", etc.
Also note that these view name patterns are checked before applying any prefixes or suffixes to the view name, so they should not include these. Usually therefore, you would specify orders/* instead of /WEB-INF/templates/orders/*.html.
viewNames
- the view names (actually view name patterns)PatternMatchUtils.simpleMatch(String[], String)
public String[] getViewNames()
Return the set of name patterns that will applied to determine whether a view name returned by a controller will be resolved by this resolver or not.
In applications configuring several view resolvers –for example, one for Thymeleaf and another one for JSP+JSTL legacy pages–, this property establishes when a view will be considered to be resolved by this view resolver and when Spring should simply ask the next resolver in the chain –according to its order– instead.
The specified view name patterns can be complete view names, but can also use the * wildcard: "index.*", "user_*", "admin/*", etc.
Also note that these view name patterns are checked before applying any prefixes or suffixes to the view name, so they should not include these. Usually therefore, you would specify orders/* instead of /WEB-INF/templates/orders/*.html.
PatternMatchUtils.simpleMatch(String[], String)
public void setExcludedViewNames(String[] excludedViewNames)
Specify names of views –patterns, in fact– that cannot be handled by this view resolver.
These patterns can be specified in the same format as those in
setViewNames(String[])
, but work as an exclusion list.
excludedViewNames
- the view names to be excluded (actually view name patterns)setViewNames(String[])
,
PatternMatchUtils.simpleMatch(String[], String)
public String[] getExcludedViewNames()
Returns the names of views –patterns, in fact– that cannot be handled by this view resolver.
These patterns can be specified in the same format as those in
setViewNames(String[])
, but work as an exclusion list.
getViewNames()
,
PatternMatchUtils.simpleMatch(String[], String)
protected org.springframework.web.servlet.View createView(String viewName, Locale locale) throws Exception
createView
in class org.springframework.web.servlet.view.AbstractCachingViewResolver
Exception
Copyright © 2016 The THYMELEAF team. All rights reserved.