org.thymeleaf.spring4.view
Class ThymeleafViewResolver

Object
  extended by org.springframework.context.support.ApplicationObjectSupport
      extended by org.springframework.web.context.support.WebApplicationObjectSupport
          extended by org.springframework.web.servlet.view.AbstractCachingViewResolver
              extended by org.thymeleaf.spring4.view.ThymeleafViewResolver
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware, org.springframework.core.Ordered, org.springframework.web.context.ServletContextAware, org.springframework.web.servlet.ViewResolver
Direct Known Subclasses:
AjaxThymeleafViewResolver

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.

Since:
1.0
Author:
Daniel Fernández

Field Summary
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.
 
Fields inherited from class org.springframework.web.servlet.view.AbstractCachingViewResolver
DEFAULT_CACHE_LIMIT
 
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
 
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
 
Constructor Summary
ThymeleafViewResolver()
           Create an instance of ThymeleafViewResolver.
 
Method Summary
 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. as relative to the web application root.
 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. as relative to the web application root.
 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.
 
Methods inherited from class org.springframework.web.servlet.view.AbstractCachingViewResolver
clearCache, getCacheKey, getCacheLimit, isCache, isCacheUnresolved, removeFromCache, resolveViewName, setCache, setCacheLimit, setCacheUnresolved
 
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, requiredContextClass, setApplicationContext
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REDIRECT_URL_PREFIX

public static final String REDIRECT_URL_PREFIX

Prefix to be used in view names (returned by controllers) for specifying an HTTP redirect.

Value: redirect:

See Also:
Constant Field Values

FORWARD_URL_PREFIX

public static final String FORWARD_URL_PREFIX

Prefix to be used in view names (returned by controllers) for specifying an HTTP forward.

Value: forward:

See Also:
Constant Field Values
Constructor Detail

ThymeleafViewResolver

public ThymeleafViewResolver()

Create an instance of ThymeleafViewResolver.

Method Detail

setViewClass

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.

Parameters:
viewClass - class that is assignable to the required view class (by default, ThmeleafView).
Since:
2.0.9

getViewClass

protected Class<? extends AbstractThymeleafView> getViewClass()

Return the view class to be used to create views.

Returns:
the view class.
Since:
2.0.9

getTemplateEngine

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.

Returns:
the template engine being used for processing templates.

setTemplateEngine

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.

Parameters:
templateEngine - the template engine to be used

getStaticVariables

public 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}.

Returns:
the map of static variables to be set into views' execution.

addStaticVariable

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}.

Parameters:
name - the name of the static variable
value - 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 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}.

Parameters:
variables - the set of variables to be added.

setOrder

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.

Parameters:
order - the order in which this view resolver will be asked to resolve the view.

getOrder

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.

Specified by:
getOrder in interface org.springframework.core.Ordered
Returns:
the order

setContentType

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.

Parameters:
contentType - the content type to be used.

getContentType

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.

Returns:
the content type currently configured

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) 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.

Parameters:
characterEncoding - the character encoding to be used (e.g. UTF-8, ISO-8859-1, etc.)

getCharacterEncoding

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.

Returns:
the character encoding to be set at a view-resolver-wide level.

setRedirectContextRelative

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".

Parameters:
redirectContextRelative - whether redirect URLs should be considered context-relative or not.
See Also:
RedirectView.setContextRelative(boolean)

isRedirectContextRelative

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.

Returns:
true if redirect URLs will be considered relative to context, false if not.
See Also:
RedirectView.setContextRelative(boolean)

setRedirectHttp10Compatible

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"

Parameters:
redirectHttp10Compatible - true if redirects should stay compatible with HTTP 1.0 clients, false if not.
See Also:
RedirectView.setHttp10Compatible(boolean)

isRedirectHttp10Compatible

public boolean isRedirectHttp10Compatible()

Return whether redirects should stay compatible with HTTP 1.0 clients.

Default is true.

Returns:
whether redirect responses should stay compatible with HTTP 1.0 clients.
See Also:
RedirectView.setHttp10Compatible(boolean)

setViewNames

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.

Parameters:
viewNames - the view names (actually view name patterns)
See Also:
PatternMatchUtils.simpleMatch(String[], String)

getViewNames

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.

Returns:
the view name patterns
See Also:
PatternMatchUtils.simpleMatch(String[], String)

setExcludedViewNames

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.

Parameters:
excludedViewNames - the view names to be excluded (actually view name patterns)
See Also:
setViewNames(String[]), PatternMatchUtils.simpleMatch(String[], String)

getExcludedViewNames

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.

Returns:
the excluded view name patterns
See Also:
getViewNames(), PatternMatchUtils.simpleMatch(String[], String)

canHandle

protected boolean canHandle(String viewName,
                            Locale locale)

createView

protected org.springframework.web.servlet.View createView(String viewName,
                                                          Locale locale)
                                                   throws Exception
Overrides:
createView in class org.springframework.web.servlet.view.AbstractCachingViewResolver
Throws:
Exception

loadView

protected org.springframework.web.servlet.View loadView(String viewName,
                                                        Locale locale)
                                                 throws Exception
Specified by:
loadView in class org.springframework.web.servlet.view.AbstractCachingViewResolver
Throws:
Exception


Copyright © 2013 The THYMELEAF team. All Rights Reserved.