org.springframework.web.reactive.result.view.freemarker

## Class FreeMarkerView

- java.lang.Object
- - org.springframework.web.reactive.result.view.AbstractView
  - - org.springframework.web.reactive.result.view.AbstractUrlBasedView
    - - org.springframework.web.reactive.result.view.freemarker.FreeMarkerView

- 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 Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static String` | `SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE` Attribute name of the `RequestContext` instance in the template model, available to Spring's macros — for example, for creating `BindStatus` objects.

    - ### Fields inherited from class org.springframework.web.reactive.result.view.AbstractView

      `logger, REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME`

    - ### Fields inherited from interface org.springframework.web.reactive.result.view.View

      `BINDING_CONTEXT_ATTRIBUTE`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `FreeMarkerView()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `afterPropertiesSet()` Invoked by the containing `BeanFactory` after it has set all bean properties and satisfied `BeanFactoryAware`, `ApplicationContextAware` etc.
    `protected FreeMarkerConfig` | `autodetectConfiguration()` Autodetect a `FreeMarkerConfig` object in the `ApplicationContext`.
    `boolean` | `checkResourceExists(Locale locale)` Check that the FreeMarker template used for this view exists and is valid.
    `protected freemarker.template.Configuration` | `getConfiguration()` Get the FreeMarker `Configuration` used by this view.
    `protected String` | `getEncoding()` Get the encoding for the FreeMarker template.
    `protected reactor.core.publisher.Mono<Map<String,Object>>` | `getModelAttributes(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.
    `protected freemarker.template.ObjectWrapper` | `getObjectWrapper()` Get the configured FreeMarker `ObjectWrapper`, or the default wrapper if none specified.
    `protected freemarker.template.Template` | `getTemplate(Locale locale)` Get the FreeMarker template for the given locale, to be rendered by this view.
    `protected freemarker.template.SimpleHash` | `getTemplateModel(Map<String,Object> model, ServerWebExchange exchange)` Build a FreeMarker template model for the given model map.
    `protected freemarker.template.Configuration` | `obtainConfiguration()` Obtain the FreeMarker `Configuration` for actual use.
    `protected reactor.core.publisher.Mono<Void>` | `renderInternal(Map<String,Object> renderAttributes, MediaType contentType, ServerWebExchange exchange)` Subclasses must implement this method to actually render the view.
    `void` | `setConfiguration(freemarker.template.Configuration configuration)` Set the FreeMarker `Configuration` to be used by this view.
    `void` | `setEncoding(String encoding)` Set the encoding of the FreeMarker template file.
    `void` | `setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers)` Set whether to expose a `RequestContext` for use by Spring's macro library, under the name "springMacroRequestContext".

    - ### Methods inherited from class org.springframework.web.reactive.result.view.AbstractUrlBasedView

      `getUrl, setUrl, toString`

    - ### Methods inherited from class org.springframework.web.reactive.result.view.AbstractView

      `createRequestContext, formatViewName, getApplicationContext, getBeanName, getDefaultCharset, getRequestContextAttribute, getRequestDataValueProcessor, getSupportedMediaTypes, obtainApplicationContext, render, resolveAsyncAttributes, resolveAsyncAttributes, setApplicationContext, setBeanName, setDefaultCharset, setRequestContextAttribute, setSupportedMediaTypes`

    - ### Methods inherited from class java.lang.Object

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

    - ### Methods inherited from interface org.springframework.web.reactive.result.view.View

      `isRedirectView`

- - ### Field Detail

    - #### SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE

          public static final String SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE

      Attribute name of the `RequestContext` instance in the template model, available to Spring's macros — for example, for creating `BindStatus` objects.
      Since:
        5.2
      See Also:
        `setExposeSpringMacroHelpers(boolean)`, Constant Field Values

  - ### Constructor Detail

    - #### FreeMarkerView

          public FreeMarkerView()

  - ### Method Detail

    - #### 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:
        `getConfiguration()`

    - #### 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:
        `SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE`

    - #### 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:
        `setConfiguration(freemarker.template.Configuration)`

    - #### 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:
        `setExposeSpringMacroHelpers(boolean)`, `AbstractView.getModelAttributes(Map, ServerWebExchange)`

    - #### 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`