Class ThymeleafViewResolver

Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.view.AbstractCachingViewResolver
org.thymeleaf.spring5.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 WebMVC 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:
3.0.3
Author:
Daniel Fernández
  • Nested Class Summary

    Nested classes/interfaces inherited from class org.springframework.web.servlet.view.AbstractCachingViewResolver

    org.springframework.web.servlet.view.AbstractCachingViewResolver.CacheFilter
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Prefix to be used in view names (returned by controllers) for specifying an HTTP forward.
    static final String
    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

    Constructors
    Constructor
    Description
    Create an instance of ThymeleafViewResolver.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Add a new static variable.
    protected boolean
    canHandle(String viewName, Locale locale)
     
    protected org.springframework.web.servlet.View
    createView(String viewName, Locale locale)
     
    boolean
    Return whether this view resolver should always process forwards and redirects independently of the value of the viewNames property.
    Returns the character encoding set to be used for all views resolved by this view resolver.
    Returns the content type that will be set into views resolved by this view resolver.
    Returns the names of views –patterns, in fact– that cannot be handled by this view resolver.
    boolean
    Returns whether the configured content type should be forced instead of attempting a smart content type application based on template name.
    int
    Returns the order in which this view resolver will be queried.
    boolean
    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.
    Return the static variables, which will be available at the context every time a view resolved by this ViewResolver is processed.
    Returns the Thymeleaf template engine instance to be used for the execution of templates.
    protected Class<? extends AbstractThymeleafView>
    Return the view class to be used to create views.
    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
    Return whether to interpret a given redirect URL that starts with a slash ("/") as relative to the current ServletContext, i.e.
    boolean
    Return whether redirects should stay compatible with HTTP 1.0 clients.
    protected org.springframework.web.servlet.View
    loadView(String viewName, Locale locale)
     
    void
    setAlwaysProcessRedirectAndForward(boolean alwaysProcessRedirectAndForward)
    Set whether this view resolver should always process forwards and redirects independently of the value of the viewNames property.
    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
    setForceContentType(boolean forceContentType)
    Sets whether the configured content type should be forced instead of attempting a smart content type application based on template name.
    void
    setOrder(int order)
    Specify the order in which this view resolver will be queried.
    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
    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
    Sets a set of static variables, which will be available at the context every time a view resolved by this ViewResolver is processed.
    void
    Sets the Template Engine instance to be used for processing templates.
    void
    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, getCacheFilter, getCacheKey, getCacheLimit, isCache, isCacheUnresolved, removeFromCache, resolveViewName, setCache, setCacheFilter, 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, obtainApplicationContext, requiredContextClass, setApplicationContext

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • 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:
    • 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:
  • Constructor Details

    • ThymeleafViewResolver

      public ThymeleafViewResolver()

      Create an instance of ThymeleafViewResolver.

  • Method Details

    • 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, ThymeleafView).
    • getViewClass

      protected Class<? extends AbstractThymeleafView> getViewClass()

      Return the view class to be used to create views.

      Returns:
      the view class.
    • getTemplateEngine

      public ISpringTemplateEngine getTemplateEngine()

      Returns the Thymeleaf template engine instance to be used for the execution of templates.

      Returns:
      the template engine being used for processing templates.
    • setTemplateEngine

      public void setTemplateEngine(ISpringTemplateEngine templateEngine)

      Sets the Template Engine instance to be used for processing templates.

      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 Web 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 Web 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
    • 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
    • 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)
    • setAlwaysProcessRedirectAndForward

      public void setAlwaysProcessRedirectAndForward(boolean alwaysProcessRedirectAndForward)

      Set whether this view resolver should always process forwards and redirects independently of the value of the viewNames property.

      When this flag is set to true (default value), any view name that starts with the redirect: or forward: prefixes will be resolved by this ViewResolver even if the view names would not match what is established at the viewNames property.

      Note that the behaviour of resolving view names with these prefixes is exactly the same with this flag set to true or false (perform an HTTP redirect or forward to an internal JSP resource). The only difference is whether the prefixes have to be explicitly specified at viewNames or not.

      Default value is true.

      Parameters:
      alwaysProcessRedirectAndForward - true if redirects and forwards are always processed, false if this will depend on what is established at the viewNames property.
    • getAlwaysProcessRedirectAndForward

      public boolean getAlwaysProcessRedirectAndForward()

      Return whether this view resolver should always process forwards and redirects independently of the value of the viewNames property.

      When this flag is set to true (default value), any view name that starts with the redirect: or forward: prefixes will be resolved by this ViewResolver even if the view names would not match what is established at the viewNames property.

      Note that the behaviour of resolving view names with these prefixes is exactly the same with this flag set to true or false (perform an HTTP redirect or forward to an internal JSP resource). The only difference is whether the prefixes have to be explicitly specified at viewNames or not.

      Default value is true.

      Returns:
      whether redirects and forwards will be always processed by this view resolver or else only when they are matched by the viewNames property.
    • 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 to false 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 to false 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
    • 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:
    • 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:
    • 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