Class FreeMarkerView
- All Implemented Interfaces:
Aware
,BeanNameAware
,InitializingBean
,ApplicationContextAware
,View
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
Modifier and TypeFieldDescriptionstatic final String
Attribute name of theRequestContext
instance in the template model, available to Spring's macros — for example, for creatingBindStatus
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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.protected FreeMarkerConfig
Autodetect aFreeMarkerConfig
object in theApplicationContext
.boolean
checkResourceExists
(Locale locale) Check that the FreeMarker template used for this view exists and is valid.protected freemarker.template.Configuration
Get the FreeMarkerConfiguration
used by this view.protected String
Get the encoding for the FreeMarker template.getModelAttributes
(Map<String, ?> model, ServerWebExchange exchange) Prepare the model to use for rendering by potentially exposing aRequestContext
for use in Spring FreeMarker macros and then delegating to the inherited implementation of this method.protected freemarker.template.ObjectWrapper
Get the configured FreeMarkerObjectWrapper
, 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
Obtain the FreeMarkerConfiguration
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 FreeMarkerConfiguration
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 aRequestContext
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, 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 Details
-
SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE
Attribute name of theRequestContext
instance in the template model, available to Spring's macros — for example, for creatingBindStatus
objects.- Since:
- 5.2
- See Also:
-
-
Constructor Details
-
FreeMarkerView
public FreeMarkerView()
-
-
Method Details
-
setConfiguration
Set the FreeMarkerConfiguration
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
Get the FreeMarkerConfiguration
used by this view. -
obtainConfiguration
protected freemarker.template.Configuration obtainConfiguration()Obtain the FreeMarkerConfiguration
for actual use.- Returns:
- the FreeMarker configuration (never
null
) - Throws:
IllegalStateException
- in case of noConfiguration
object set- See Also:
-
setEncoding
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 FreeMarkerConfiguration
rather than per template if all your templates share a common encoding. -
getEncoding
Get the encoding for the FreeMarker template. -
setExposeSpringMacroHelpers
public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers) Set whether to expose aRequestContext
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
Description copied from interface:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,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 interfaceInitializingBean
- Overrides:
afterPropertiesSet
in classAbstractUrlBasedView
- Throws:
Exception
- in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason
-
autodetectConfiguration
Autodetect aFreeMarkerConfig
object in theApplicationContext
.- Returns:
- the
FreeMarkerConfig
instance to use for this view - Throws:
BeansException
- if noFreeMarkerConfig
instance could be found- See Also:
-
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.
- Specified by:
checkResourceExists
in classAbstractUrlBasedView
- Parameters:
locale
- the desired Locale that we're looking for- Returns:
false
if the resource existsfalse
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 aRequestContext
for use in Spring FreeMarker macros and then delegating to the inherited implementation of this method.- Overrides:
getModelAttributes
in classAbstractView
- 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 classAbstractView
- Parameters:
renderAttributes
- combined output Map (nevernull
), with dynamic values taking precedence over static attributescontentType
- the content type selected to render with, which should match one of the supported media typesexchange
- 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 renderingexchange
- current exchange- Returns:
- the FreeMarker template model, as a
SimpleHash
or subclass thereof
-
getObjectWrapper
protected freemarker.template.ObjectWrapper getObjectWrapper()Get the configured FreeMarkerObjectWrapper
, or the default wrapper if none specified.- See Also:
-
Configurable.getObjectWrapper()
-
getTemplate
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
-