All Implemented Interfaces:
Aware, BeanNameAware, InitializingBean, ApplicationContextAware, View

public class FreeMarkerView extends AbstractUrlBasedView
A View implementation that uses the FreeMarker template engine.

Exposes the following configuration properties:

  • url: the location of the FreeMarker template relative to the FreeMarkerConfigurer's templateLoaderPath.
  • encoding: the encoding used to decode byte sequences to character sequences when reading the FreeMarker template file. Default is determined by the FreeMarker Configuration.

Depends on a single FreeMarkerConfig object such as FreeMarkerConfigurer being accessible in the application context. Alternatively the FreeMarker Configuration can be set directly via setConfiguration(freemarker.template.Configuration).

Note: To ensure that the correct encoding is used when rendering the response as well as when the client reads the response, the following steps must be taken.

Note, however, that FreeMarkerConfigurer sets the default encoding in the FreeMarker Configuration to "UTF-8" and that AbstractView sets the supported media type to "text/html;charset=UTF-8" by default. Thus, those default values are likely suitable for most applications.

Note: Spring's FreeMarker support requires FreeMarker 2.3.21 or higher.

Since:
5.0
Author:
Rossen Stoyanchev, Sam Brannen
  • Field Details

  • Constructor Details

    • FreeMarkerView

      public FreeMarkerView()
  • Method Details

    • setConfiguration

      public void setConfiguration(@Nullable freemarker.template.Configuration configuration)
      Set the FreeMarker Configuration to be used by this view.

      Typically this property is not set directly. Instead a single FreeMarkerConfig is expected in the Spring application context which is used to obtain the FreeMarker configuration.

    • getConfiguration

      @Nullable protected freemarker.template.Configuration getConfiguration()
      Get the FreeMarker Configuration used by this view.
    • obtainConfiguration

      protected freemarker.template.Configuration obtainConfiguration()
      Obtain the FreeMarker Configuration for actual use.
      Returns:
      the FreeMarker configuration (never null)
      Throws:
      IllegalStateException - in case of no Configuration object set
      See Also:
    • setEncoding

      public void setEncoding(@Nullable String encoding)
      Set the encoding used to decode byte sequences to character sequences when reading the FreeMarker template file for this view.

      Defaults to null to signal that the FreeMarker Configuration should be used to determine the encoding.

      A non-null encoding will override the default encoding determined by the FreeMarker Configuration.

      If the encoding is not explicitly set here or in the FreeMarker Configuration, FreeMarker will read template files using the platform file encoding (defined by the JVM system property file.encoding) or "utf-8" if the platform file encoding is undefined. Note, however, that FreeMarkerConfigurer sets the default encoding in the FreeMarker Configuration to "UTF-8".

      It's recommended to specify the encoding in the FreeMarker Configuration rather than per template if all your templates share a common encoding.

      Note that the specified or default encoding is not used for template rendering. Instead, an explicit encoding must be specified for the rendering process. See the note in the class-level documentation for details.

      See Also:
      • Configuration.setDefaultEncoding(java.lang.String)
      • getEncoding()
    • getEncoding

      @Nullable protected String getEncoding()
      Get the encoding used to decode byte sequences to character sequences when reading the FreeMarker template file for this view, or null to signal that the FreeMarker Configuration should be used to determine the encoding.
      See Also:
    • setExposeSpringMacroHelpers

      public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers)
      Set whether to expose a RequestContext for use by Spring's macro library, under the name "springMacroRequestContext".

      Default is true.

      Needed for Spring's FreeMarker default macros. Note that this is not required for templates that use HTML forms unless you wish to take advantage of the Spring helper macros.

      Since:
      5.2
      See Also:
    • afterPropertiesSet

      public void afterPropertiesSet() throws Exception
      Description copied from interface: InitializingBean
      Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

      This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

      Specified by:
      afterPropertiesSet in interface InitializingBean
      Overrides:
      afterPropertiesSet in class AbstractUrlBasedView
      Throws:
      Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason
    • autodetectConfiguration

      protected FreeMarkerConfig autodetectConfiguration() throws BeansException
      Autodetect a FreeMarkerConfig object in the ApplicationContext.
      Returns:
      the FreeMarkerConfig instance to use for this view
      Throws:
      BeansException - if no FreeMarkerConfig instance could be found
      See Also:
    • checkResourceExists

      public boolean checkResourceExists(Locale locale) throws Exception
      Check that the FreeMarker template used for this view exists and is valid.

      Can be overridden to customize the behavior, for example in case of multiple templates to be rendered into a single view.

      Specified by:
      checkResourceExists in class AbstractUrlBasedView
      Parameters:
      locale - the desired Locale that we're looking for
      Returns:
      false if the resource exists false if we know that it does not exist
      Throws:
      Exception - if the resource exists but is invalid (e.g. could not be parsed)
    • resourceExists

      public reactor.core.publisher.Mono<Boolean> resourceExists(Locale locale)
      Check that the FreeMarker template used for this view exists and is valid.

      Can be overridden to customize the behavior, for example in case of multiple templates to be rendered into a single view.

      Overrides:
      resourceExists in class AbstractUrlBasedView
      Parameters:
      locale - the desired Locale that we're looking for
      Returns:
      false if the resource exists false if we know that it does not exist
      Since:
      6.1
    • getModelAttributes

      protected reactor.core.publisher.Mono<Map<String,Object>> getModelAttributes(@Nullable Map<String,?> model, ServerWebExchange exchange)
      Prepare the model to use for rendering by potentially exposing a RequestContext for use in Spring FreeMarker macros and then delegating to the inherited implementation of this method.
      Overrides:
      getModelAttributes in class AbstractView
      Since:
      5.2
      See Also:
    • renderInternal

      protected reactor.core.publisher.Mono<Void> renderInternal(Map<String,Object> renderAttributes, @Nullable MediaType contentType, ServerWebExchange exchange)
      Description copied from class: AbstractView
      Subclasses must implement this method to actually render the view.
      Specified by:
      renderInternal in class AbstractView
      Parameters:
      renderAttributes - combined output Map (never null), with dynamic values taking precedence over static attributes
      contentType - the content type selected to render with, which should match one of the supported media types
      exchange - current exchange
      Returns:
      a Mono that represents when and if rendering succeeds
    • getTemplateModel

      protected freemarker.template.SimpleHash getTemplateModel(Map<String,Object> model, ServerWebExchange exchange)
      Build a FreeMarker template model for the given model map.

      The default implementation builds a SimpleHash.

      Parameters:
      model - the model to use for rendering
      exchange - current exchange
      Returns:
      the FreeMarker template model, as a SimpleHash or subclass thereof
    • getObjectWrapper

      protected freemarker.template.ObjectWrapper getObjectWrapper()
      Get the configured FreeMarker ObjectWrapper, or the default wrapper if none specified.
      See Also:
      • Configurable.getObjectWrapper()
    • getTemplate

      @Deprecated(since="6.1", forRemoval=true) protected freemarker.template.Template getTemplate(Locale locale) throws IOException
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 6.1, in favor of lookupTemplate(Locale), to be removed in 6.2
      Get the FreeMarker template for the given locale, to be rendered by this view.

      By default, the template specified by the "url" bean property will be retrieved.

      Parameters:
      locale - the current locale
      Returns:
      the FreeMarker template to render
      Throws:
      IOException
    • lookupTemplate

      protected reactor.core.publisher.Mono<freemarker.template.Template> lookupTemplate(Locale locale)
      Retrieve the FreeMarker template for the given locale, to be rendered by this view.

      By default, the template specified by the "url" bean property will be retrieved, and the returned mono will subscribe on the bounded elastic scheduler as template lookups can be blocking operations.

      Parameters:
      locale - the current locale
      Returns:
      the FreeMarker template to render
      Since:
      6.1