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(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 |
---|---|
private Configuration |
configuration |
private java.lang.String |
encoding |
logger, REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME
Constructor and Description |
---|
FreeMarkerView() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied
(and satisfied BeanFactoryAware and ApplicationContextAware).
|
protected FreeMarkerConfig |
autodetectConfiguration()
Autodetect a
FreeMarkerConfig object via the ApplicationContext. |
boolean |
checkResourceExists(java.util.Locale locale)
Check that the FreeMarker template used for this view exists and is valid.
|
private java.nio.charset.Charset |
getCharset(MediaType mediaType) |
protected Configuration |
getConfiguration()
Return the FreeMarker configuration used by this view.
|
protected java.lang.String |
getEncoding()
Return the encoding for the FreeMarker template.
|
protected ObjectWrapper |
getObjectWrapper()
Return the configured FreeMarker
ObjectWrapper , or the
default wrapper if none specified. |
protected Template |
getTemplate(java.util.Locale locale)
Retrieve the FreeMarker template for the given locale,
to be rendering by this view.
|
protected SimpleHash |
getTemplateModel(java.util.Map<java.lang.String,java.lang.Object> model,
ServerWebExchange exchange)
Build a FreeMarker template model for the given model Map.
|
protected Configuration |
obtainConfiguration()
Obtain the FreeMarker configuration for actual use.
|
protected <any> |
renderInternal(java.util.Map<java.lang.String,java.lang.Object> renderAttributes,
MediaType contentType,
ServerWebExchange exchange)
Subclasses must implement this method to actually render the view.
|
void |
setConfiguration(Configuration configuration)
Set the FreeMarker Configuration to be used by this view.
|
void |
setEncoding(java.lang.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
@Nullable private Configuration configuration
@Nullable private java.lang.String encoding
public void setConfiguration(@Nullable 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 Configuration getConfiguration()
protected Configuration obtainConfiguration()
null
)java.lang.IllegalStateException
- in case of no Configuration object setpublic void setEncoding(@Nullable java.lang.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 java.lang.String getEncoding()
public void afterPropertiesSet() throws java.lang.Exception
InitializingBean
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
afterPropertiesSet
in interface InitializingBean
afterPropertiesSet
in class AbstractUrlBasedView
java.lang.Exception
- in the event of misconfiguration (such
as failure to set an essential property) or if initialization fails.protected FreeMarkerConfig autodetectConfiguration() throws BeansException
FreeMarkerConfig
object via the ApplicationContext.BeansException
- if no Configuration instance could be foundsetConfiguration(Configuration)
public boolean checkResourceExists(java.util.Locale locale) throws java.lang.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 existjava.lang.Exception
- if the resource exists but is invalid (e.g. could not be parsed)protected <any> renderInternal(java.util.Map<java.lang.String,java.lang.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 types
.exchange
- current exchange @return Mono
to represent when
and if rendering succeedsprotected SimpleHash getTemplateModel(java.util.Map<java.lang.String,java.lang.Object> model, ServerWebExchange exchange)
The default implementation builds a SimpleHash
.
model
- the model to use for renderingexchange
- current exchangeSimpleHash
or subclass thereofprotected ObjectWrapper getObjectWrapper()
ObjectWrapper
, or the
default wrapper
if none specified.freemarker.template.Configuration#getObjectWrapper()
protected Template getTemplate(java.util.Locale locale) throws java.io.IOException
By default, the template specified by the "url" bean property will be retrieved.
locale
- the current localejava.io.IOException