Top Description Fields Constructors Methods
org.springframework.web.reactive.result.view.freemarker

public Class FreeMarkerView

extends AbstractUrlBasedView
Class Inheritance
Imports
java.io.FileNotFoundException, .IOException, .OutputStreamWriter, .Writer, java.nio.charset.Charset, java.util.HashMap, .Locale, .Map, .Optional, freemarker.core.ParseException, freemarker.template.Configuration, freemarker.template.DefaultObjectWrapperBuilder, freemarker.template.ObjectWrapper, freemarker.template.SimpleHash, freemarker.template.Template, freemarker.template.TemplateException, freemarker.template.Version, reactor.core.publisher.Mono, reactor.core.scheduler.Schedulers, org.springframework.beans.BeansException, org.springframework.beans.factory.BeanFactoryUtils, .NoSuchBeanDefinitionException, org.springframework.context.ApplicationContextException, org.springframework.context.i18n.LocaleContextHolder, org.springframework.core.io.buffer.DataBuffer, .DataBufferUtils, org.springframework.http.MediaType, org.springframework.lang.Nullable, org.springframework.util.Assert, .FastByteArrayOutputStream, .MimeType, org.springframework.web.reactive.result.view.AbstractUrlBasedView, .RequestContext, org.springframework.web.server.ServerWebExchange

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.

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.

Authors
Rossen Stoyanchev, Sam Brannen
Since
5.0

Field Summary

Modifier and TypeField and Description
private freemarker.template.Configuration
private String
private boolean
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.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Overrides org.springframework.web.reactive.result.view.AbstractUrlBasedView.afterPropertiesSet.

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

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.

protected FreeMarkerConfig

Returns:

the FreeMarkerConfig instance to use for this view
autodetectConfiguration
()

Autodetect a FreeMarkerConfig object in the ApplicationContext.

public boolean
checkResourceExists(Locale
the desired Locale that we're looking for
locale
)

Implements abstract org.springframework.web.reactive.result.view.AbstractUrlBasedView.checkResourceExists.

Check that the FreeMarker template used for this view exists and is valid.

private Charset
getCharset(MediaType mediaType)

protected freemarker.template.Configuration
getConfiguration()

Get the FreeMarker Configuration used by this view.

protected String
getEncoding()

Get the encoding for the FreeMarker template.

protected Mono<Map<String, Object>>
getModelAttributes(Map<String, ?> model, ServerWebExchange exchange)

Overrides org.springframework.web.reactive.result.view.AbstractView.getModelAttributes.

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

Returns:

the FreeMarker template to render
getTemplate
(Locale
the current locale
locale
)
Deprecated for removal since 6.1. 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.

protected freemarker.template.SimpleHash

Returns:

the FreeMarker template model, as a SimpleHash or subclass thereof
getTemplateModel
(Map<String, Object>
the model to use for rendering
model
,
ServerWebExchange
current exchange
exchange
)

Build a FreeMarker template model for the given model map.

protected Mono<freemarker.template.Template>

Returns:

the FreeMarker template to render
lookupTemplate
(Locale
the current locale
locale
)

Retrieve the FreeMarker template for the given locale, to be rendered by this view.

protected freemarker.template.Configuration

Returns:

the FreeMarker configuration (never null)
obtainConfiguration
()

Obtain the FreeMarker Configuration for actual use.

protected Mono<Void>
renderInternal(Map<String, Object>
combined output Map (never null), with dynamic values taking precedence over static attributes
renderAttributes
,
MediaType
the content type selected to render with, which should match one of the supported media types
contentType
,
ServerWebExchange
current exchange
exchange
)

Implements abstract org.springframework.web.reactive.result.view.AbstractView.renderInternal.

Subclasses must implement this method to actually render the view.

public Mono<Boolean>
resourceExists(Locale
the desired Locale that we're looking for
locale
)

Overrides org.springframework.web.reactive.result.view.AbstractUrlBasedView.resourceExists.

Check that the FreeMarker template used for this view exists and is valid.

public void
setConfiguration(freemarker.template.Configuration configuration)

Set the FreeMarker Configuration to be used by this view.

public void
setEncoding(String encoding)

Set the encoding of the FreeMarker template file.

public void
setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers)

Set whether to expose a RequestContext for use by Spring's macro library, under the name springMacroRequestContext.

Inherited from org.springframework.web.reactive.result.view.AbstractUrlBasedView:
getUrlsetUrltoString

Field Detail

configurationback to summary
private freemarker.template.Configuration configuration
Annotations
@Nullable
encodingback to summary
private String encoding
Annotations
@Nullable
exposeSpringMacroHelpersback to summary
private boolean exposeSpringMacroHelpers
SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTEback to summary
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)

Constructor Detail

FreeMarkerViewback to summary
public FreeMarkerView()

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet() throws Exception

Overrides org.springframework.web.reactive.result.view.AbstractUrlBasedView.afterPropertiesSet.

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Doc from org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

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.

Annotations
@Override
Exceptions
Exception:
in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason
autodetectConfigurationback to summary
protected FreeMarkerConfig autodetectConfiguration() throws BeansException

Autodetect a FreeMarkerConfig object in the ApplicationContext.

Returns:FreeMarkerConfig

the FreeMarkerConfig instance to use for this view

Exceptions
BeansException:
if no FreeMarkerConfig instance could be found
See Also
setConfiguration
checkResourceExistsback to summary
public boolean checkResourceExists(Locale locale) throws Exception

Implements abstract org.springframework.web.reactive.result.view.AbstractUrlBasedView.checkResourceExists.

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.

Parameters
locale:Locale

Doc from org.springframework.web.reactive.result.view.AbstractUrlBasedView.checkResourceExists.

the desired Locale that we're looking for

Returns:boolean

Doc from org.springframework.web.reactive.result.view.AbstractUrlBasedView.checkResourceExists.

false if the resource exists false if we know that it does not exist

Annotations
@Override
Exceptions
Exception:

Doc from org.springframework.web.reactive.result.view.AbstractUrlBasedView.checkResourceExists.

if the resource exists but is invalid (e.g. could not be parsed)

getCharsetback to summary
private Charset getCharset(MediaType mediaType)
getConfigurationback to summary
protected freemarker.template.Configuration getConfiguration()

Get the FreeMarker Configuration used by this view.

Annotations
@Nullable
getEncodingback to summary
protected String getEncoding()

Get the encoding for the FreeMarker template.

Annotations
@Nullable
getModelAttributesback to summary
protected Mono<Map<String, Object>> getModelAttributes(Map<String, ?> model, ServerWebExchange exchange)

Overrides org.springframework.web.reactive.result.view.AbstractView.getModelAttributes.

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.

Annotations
@Override
Since
5.2
See Also
setExposeSpringMacroHelpers(boolean), org.springframework.web.reactive.result.view.AbstractView#getModelAttributes(Map, ServerWebExchange)
getObjectWrapperback to summary
protected freemarker.template.ObjectWrapper getObjectWrapper()

Get the configured FreeMarker ObjectWrapper, or the default wrapper if none specified.

See Also
freemarker.template.Configuration#getObjectWrapper()
getTemplateback to summary
protected freemarker.template.Template getTemplate(Locale locale) throws IOException

Deprecated

for removal since 6.1.

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:Locale

the current locale

Returns:freemarker.template.Template

the FreeMarker template to render

Annotations
@Deprecated
since:6.1
forRemoval:true
getTemplateModelback to summary
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:Map<String, Object>

the model to use for rendering

exchange:ServerWebExchange

current exchange

Returns:freemarker.template.SimpleHash

the FreeMarker template model, as a SimpleHash or subclass thereof

lookupTemplateback to summary
protected 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:Locale

the current locale

Returns:Mono<freemarker.template.Template>

the FreeMarker template to render

Since
6.1
obtainConfigurationback to summary
protected freemarker.template.Configuration obtainConfiguration()

Obtain the FreeMarker Configuration for actual use.

Returns:freemarker.template.Configuration

the FreeMarker configuration (never null)

Exceptions
IllegalStateException:
in case of no Configuration object set
See Also
getConfiguration()
renderInternalback to summary
protected Mono<Void> renderInternal(Map<String, Object> renderAttributes, MediaType contentType, ServerWebExchange exchange)

Implements abstract org.springframework.web.reactive.result.view.AbstractView.renderInternal.

Doc from org.springframework.web.reactive.result.view.AbstractView.renderInternal.

Subclasses must implement this method to actually render the view.

Parameters
renderAttributes:Map<String, Object>

combined output Map (never null), with dynamic values taking precedence over static attributes

contentType:MediaType

the content type selected to render with, which should match one of the supported media types

exchange:ServerWebExchange

current exchange

Returns:Mono<Void>

a Mono that represents when and if rendering succeeds

Annotations
@Override
resourceExistsback to summary
public Mono<Boolean> resourceExists(Locale locale)

Overrides org.springframework.web.reactive.result.view.AbstractUrlBasedView.resourceExists.

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.

Parameters
locale:Locale

Doc from org.springframework.web.reactive.result.view.AbstractUrlBasedView.resourceExists.

the desired Locale that we're looking for

Returns:Mono<Boolean>

Doc from org.springframework.web.reactive.result.view.AbstractUrlBasedView.resourceExists.

false if the resource exists false if we know that it does not exist

Annotations
@Override
Since
6.1
setConfigurationback to summary
public void setConfiguration(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.

setEncodingback to summary
public void setEncoding(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.

setExposeSpringMacroHelpersback to summary
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