public class FreeMarkerView extends AbstractUrlBasedView
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.
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. |
logger, REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME
BINDING_CONTEXT_ATTRIBUTE
Constructor and Description |
---|
FreeMarkerView() |
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". |
getUrl, setUrl, toString
createRequestContext, formatViewName, getApplicationContext, getBeanName, getDefaultCharset, getRequestContextAttribute, getRequestDataValueProcessor, getSupportedMediaTypes, obtainApplicationContext, render, resolveAsyncAttributes, resolveAsyncAttributes, setApplicationContext, setBeanName, setDefaultCharset, setRequestContextAttribute, setSupportedMediaTypes
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
isRedirectView
public static final String SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE
RequestContext
instance in the template model,
available to Spring's macros — for example, for creating
BindStatus
objects.setExposeSpringMacroHelpers(boolean)
,
Constant Field Valuespublic void setConfiguration(@Nullable freemarker.template.Configuration configuration)
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.
@Nullable protected freemarker.template.Configuration getConfiguration()
Configuration
used by this view.protected freemarker.template.Configuration obtainConfiguration()
Configuration
for actual use.null
)IllegalStateException
- in case of no Configuration
object setgetConfiguration()
public void setEncoding(@Nullable String encoding)
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.
public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers)
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.
SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE
public void afterPropertiesSet() throws Exception
InitializingBean
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.
afterPropertiesSet
in interface InitializingBean
afterPropertiesSet
in class AbstractUrlBasedView
Exception
- in the event of misconfiguration (such as failure to set an
essential property) or if initialization fails for any other reasonprotected FreeMarkerConfig autodetectConfiguration() throws BeansException
FreeMarkerConfig
object in the ApplicationContext
.FreeMarkerConfig
instance to use for this viewBeansException
- if no FreeMarkerConfig
instance could be foundsetConfiguration(freemarker.template.Configuration)
public boolean checkResourceExists(Locale locale) throws Exception
Can be overridden to customize the behavior, for example in case of multiple templates to be rendered into a single view.
checkResourceExists
in class AbstractUrlBasedView
locale
- the desired Locale that we're looking forfalse
if the resource exists
false
if we know that it does not existException
- if the resource exists but is invalid (e.g. could not be parsed)protected reactor.core.publisher.Mono<Map<String,Object>> getModelAttributes(@Nullable Map<String,?> model, ServerWebExchange exchange)
RequestContext
for use in Spring FreeMarker macros and then
delegating to the inherited implementation of this method.getModelAttributes
in class AbstractView
setExposeSpringMacroHelpers(boolean)
,
AbstractView.getModelAttributes(Map, ServerWebExchange)
protected reactor.core.publisher.Mono<Void> renderInternal(Map<String,Object> renderAttributes, @Nullable MediaType contentType, ServerWebExchange exchange)
AbstractView
renderInternal
in class AbstractView
renderAttributes
- combined output Map (never null
),
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 exchangeMono
that represents when and if rendering succeedsprotected freemarker.template.SimpleHash getTemplateModel(Map<String,Object> model, ServerWebExchange exchange)
The default implementation builds a SimpleHash
.
model
- the model to use for renderingexchange
- current exchangeSimpleHash
or subclass thereofprotected freemarker.template.ObjectWrapper getObjectWrapper()
ObjectWrapper
, or the
default wrapper if none specified.Configurable.getObjectWrapper()
protected freemarker.template.Template getTemplate(Locale locale) throws IOException
By default, the template specified by the "url" bean property will be retrieved.
locale
- the current localeIOException