Class AbstractThymeleafView

Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.thymeleaf.spring6.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 final String
    Default charset set to ISO-8859-1 for compatibility reasons with Spring's AbstractView.
    static final boolean
    By default Thymeleaf will not wait until a template is fully processed and rendered before starting to output its results.

    Fields inherited from class org.springframework.context.support.ApplicationObjectSupport

    logger

    Fields inherited from interface org.springframework.web.servlet.View

    PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Creates a new instance of ThymeleafView.
    protected
    Creates a new instance of ThymeleafView, specifying the template name.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected static void
    addRequestContextAsVariable(Map<String,Object> model, String variableName, org.springframework.web.servlet.support.RequestContext requestContext)
     
    void
    Add a new static variable.
    Returns the bean name.
    Returns the character encoding set to be used for rendering this view.
    Returns the content type that will used for this view.
    boolean
    Returns whether the configured content type should be forced instead of attempting a smart content type application based on template name.
    protected Locale
    Returns the locale to be used for template processing.
    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 this view is processed.
    Returns the template engine instance –a SpringTemplateEngine instance, specifically– to be used for processing the template specified by this view object.
    Returns the name of the template being processed by this view object.
    protected boolean
     
    protected boolean
     
    protected boolean
     
    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
    Sets a set of static variables, which will be available at the context when this view is processed.
    protected void
    Sets the template engine instance –a SpringTemplateEngine 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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.springframework.web.servlet.View

    render
  • Field Details

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

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

    • getContentType

      public String getContentType()

      Returns the content type that will used for this view.

      Content type will be computed this way:

      Specified by:
      getContentType in interface org.springframework.web.servlet.View
      Returns:
      the content type
      See Also:
    • setContentType

      public void setContentType(String contentType)

      Sets the content type that will used for this view.

      Content type will be computed this way:

      Parameters:
      contentType - the content type to be used.
      See Also:
    • 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 the setCharacterEncoding(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 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
    • 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 interface org.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 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 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.
    • addRequestContextAsVariable

      protected static void addRequestContextAsVariable(Map<String,Object> model, String variableName, org.springframework.web.servlet.support.RequestContext requestContext) throws jakarta.servlet.ServletException
      Throws:
      jakarta.servlet.ServletException