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

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

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

The url property is the location of the FreeMarker template relative to the FreeMarkerConfigurer's templateLoaderPath.

Note: Spring's FreeMarker support requires FreeMarker 2.3 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 of the FreeMarker template file.

      By default 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.

    • getEncoding

      @Nullable protected String getEncoding()
      Get the encoding for the FreeMarker template.
    • 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)
    • 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

      protected freemarker.template.Template getTemplate(Locale locale) throws IOException
      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