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
FieldsModifier and TypeFieldDescriptionstatic final StringAttribute name of theRequestContextinstance in the template model, available to Spring's macros — for example, for creatingBindStatusobjects.Fields inherited from class org.springframework.web.reactive.result.view.AbstractView
logger, REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAMEFields inherited from interface org.springframework.web.reactive.result.view.View
BINDING_CONTEXT_ATTRIBUTE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.protected FreeMarkerConfigAutodetect aFreeMarkerConfigobject in theApplicationContext.booleancheckResourceExists(Locale locale) Check that the FreeMarker template used for this view exists and is valid.protected freemarker.template.ConfigurationGet the FreeMarkerConfigurationused by this view.protected StringGet the encoding for the FreeMarker template.getModelAttributes(Map<String, ?> model, ServerWebExchange exchange) Prepare the model to use for rendering by potentially exposing aRequestContextfor use in Spring FreeMarker macros and then delegating to the inherited implementation of this method.protected freemarker.template.ObjectWrapperGet the configured FreeMarkerObjectWrapper, or the default wrapper if none specified.protected freemarker.template.TemplategetTemplate(Locale locale) Get the FreeMarker template for the given locale, to be rendered by this view.protected freemarker.template.SimpleHashgetTemplateModel(Map<String, Object> model, ServerWebExchange exchange) Build a FreeMarker template model for the given model map.protected freemarker.template.ConfigurationObtain the FreeMarkerConfigurationfor 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.voidsetConfiguration(freemarker.template.Configuration configuration) Set the FreeMarkerConfigurationto be used by this view.voidsetEncoding(String encoding) Set the encoding of the FreeMarker template file.voidsetExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers) Set whether to expose aRequestContextfor use by Spring's macro library, under the name "springMacroRequestContext".Methods inherited from class org.springframework.web.reactive.result.view.AbstractUrlBasedView
getUrl, setUrl, toStringMethods 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, setSupportedMediaTypesMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.springframework.web.reactive.result.view.View
isRedirectView
-
Field Details
-
SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE
Attribute name of theRequestContextinstance in the template model, available to Spring's macros — for example, for creatingBindStatusobjects.- Since:
- 5.2
- See Also:
-
-
Constructor Details
-
FreeMarkerView
public FreeMarkerView()
-
-
Method Details
-
setConfiguration
Set the FreeMarkerConfigurationto be used by this view.Typically this property is not set directly. Instead a single
FreeMarkerConfigis expected in the Spring application context which is used to obtain the FreeMarker configuration. -
getConfiguration
Get the FreeMarkerConfigurationused by this view. -
obtainConfiguration
protected freemarker.template.Configuration obtainConfiguration()Obtain the FreeMarkerConfigurationfor actual use.- Returns:
- the FreeMarker configuration (never
null) - Throws:
IllegalStateException- in case of noConfigurationobject set- See Also:
-
setEncoding
Set the encoding of the FreeMarker template file.By default
FreeMarkerConfigurersets the default encoding in the FreeMarker configuration to "UTF-8". It's recommended to specify the encoding in the FreeMarkerConfigurationrather 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 aRequestContextfor 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:InitializingBeanInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.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:
afterPropertiesSetin interfaceInitializingBean- Overrides:
afterPropertiesSetin 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 aFreeMarkerConfigobject in theApplicationContext.- Returns:
- the
FreeMarkerConfiginstance to use for this view - Throws:
BeansException- if noFreeMarkerConfiginstance 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:
checkResourceExistsin classAbstractUrlBasedView- Parameters:
locale- the desired Locale that we're looking for- Returns:
falseif the resource existsfalseif 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 aRequestContextfor use in Spring FreeMarker macros and then delegating to the inherited implementation of this method.- Overrides:
getModelAttributesin 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:AbstractViewSubclasses must implement this method to actually render the view.- Specified by:
renderInternalin 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
Monothat 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
SimpleHashor 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
-