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 set 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.
logger, REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME
Constructor and Description |
---|
FreeMarkerView() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet() |
protected FreeMarkerConfig |
autodetectConfiguration()
Autodetect a
FreeMarkerConfig object via the ApplicationContext. |
boolean |
checkResourceExists(Locale locale)
Check that the FreeMarker template used for this view exists and is valid.
|
protected freemarker.template.Configuration |
getConfiguration()
Return the FreeMarker configuration used by this view.
|
protected String |
getEncoding()
Return the encoding for the FreeMarker template.
|
protected freemarker.template.ObjectWrapper |
getObjectWrapper()
Return the configured FreeMarker
ObjectWrapper , or the
default wrapper if none specified. |
protected freemarker.template.Template |
getTemplate(Locale locale)
Retrieve the FreeMarker template for the given locale,
to be rendering by this view.
|
protected freemarker.template.SimpleHash |
getTemplateModel(Map<String,Object> model,
org.springframework.web.server.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,
org.springframework.http.MediaType contentType,
org.springframework.web.server.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.
|
getUrl, setUrl, toString
createRequestContext, getApplicationContext, getDefaultCharset, getModelAttributes, getRequestContextAttribute, getRequestDataValueProcessor, getSupportedMediaTypes, obtainApplicationContext, render, resolveAsyncAttributes, setApplicationContext, setDefaultCharset, setRequestContextAttribute, setSupportedMediaTypes
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
isRedirectView
public void setConfiguration(@Nullable freemarker.template.Configuration configuration)
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()
protected freemarker.template.Configuration obtainConfiguration()
null
)IllegalStateException
- in case of no Configuration object setpublic 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.
@Nullable protected String getEncoding()
public void afterPropertiesSet() throws Exception
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
afterPropertiesSet
in class AbstractUrlBasedView
Exception
protected FreeMarkerConfig autodetectConfiguration() throws org.springframework.beans.BeansException
FreeMarkerConfig
object via the ApplicationContext.org.springframework.beans.BeansException
- if no Configuration 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<Void> renderInternal(Map<String,Object> renderAttributes, @Nullable org.springframework.http.MediaType contentType, org.springframework.web.server.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 types
.exchange
- current exchange @return Mono
to represent when
and if rendering succeedsprotected freemarker.template.SimpleHash getTemplateModel(Map<String,Object> model, org.springframework.web.server.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