Top Description Inners Fields Constructors Methods
org.springframework.web.servlet.view.freemarker

public Class FreeMarkerView

extends AbstractTemplateView
Class Inheritance
Imports
java.io.FileNotFoundException, .IOException, java.util.Locale, .Map, freemarker.core.ParseException, freemarker.template.Configuration, freemarker.template.DefaultObjectWrapperBuilder, freemarker.template.ObjectWrapper, freemarker.template.SimpleHash, freemarker.template.Template, freemarker.template.TemplateException, freemarker.template.TemplateModel, freemarker.template.TemplateModelException, jakarta.servlet.ServletContext, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.beans.BeansException, org.springframework.beans.factory.BeanFactoryUtils, .NoSuchBeanDefinitionException, org.springframework.context.ApplicationContextException, org.springframework.lang.Nullable, org.springframework.util.Assert, org.springframework.web.servlet.support.RequestContextUtils, org.springframework.web.servlet.view.AbstractTemplateView

View using the FreeMarker template engine.

Exposes the following JavaBean properties:

Depends on a single FreeMarkerConfig object such as FreeMarkerConfigurer being accessible in the current web application context, with any bean name. Alternatively, you can set the FreeMarker Configuration object as a bean property. See setConfiguration for more details on the impacts of this approach.

Note

Spring's FreeMarker support requires FreeMarker 2.3 or higher. As of Spring Framework 6.0, FreeMarker templates are rendered in a minimal fashion without JSP support, just exposing request attributes in addition to the MVC-provided model map for alignment with common Servlet resources.

Authors
Darren Davison, Juergen Hoeller
Since
03.03.2004
See Also
setUrl, setExposeSpringMacroHelpers, setEncoding, setConfiguration, FreeMarkerConfig, FreeMarkerConfigurer

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
FreeMarkerView.RequestHashModel

Extension of FreeMarker SimpleHash, adding a fallback to request attributes.

Field Summary

Modifier and TypeField and Description
private freemarker.template.Configuration
private String
Inherited from org.springframework.web.servlet.view.AbstractTemplateView:
SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected FreeMarkerConfig

Returns:

the Configuration instance to use for FreeMarkerViews
autodetectConfiguration
()

Autodetect a FreeMarkerConfig object via the ApplicationContext.

protected freemarker.template.SimpleHash

Returns:

the FreeMarker template model, as a SimpleHash or subclass thereof
buildTemplateModel
(Map<String, Object>
the model to use for rendering
model
,
HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current servlet response
response
)

Build a FreeMarker template model for the given model Map.

public boolean
checkResource(Locale
the desired Locale that we're looking for
locale
)

Overrides org.springframework.web.servlet.view.AbstractUrlBasedView.checkResource.

Check that the FreeMarker template used for this view exists and is valid.

protected void
doRender(Map<String, Object>
the model to use for rendering
model
,
HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current servlet response
response
)

Render the FreeMarker view to the given response, using the given model map which contains the complete template model to use.

protected void
exposeHelpers(Map<String, Object>
the model that will be passed to the template at merge time
model
,
HttpServletRequest
current HTTP request
request
)

Expose helpers unique to each rendering operation.

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

Returns:

the FreeMarker template to render
getTemplate
(Locale
the current locale
locale
)

Retrieve the FreeMarker template for the given locale, to be rendering by this view.

protected freemarker.template.Template

Returns:

the FreeMarker template
getTemplate
(String
the file name of the desired template
name
,
Locale
the current locale
locale
)

Retrieve the FreeMarker template specified by the given name, using the encoding specified by the "encoding" bean property.

protected void
initServletContext(ServletContext
the ServletContext that this application object runs in (never null)
servletContext
)

Overrides org.springframework.web.context.support.WebApplicationObjectSupport.initServletContext.

Invoked on startup.

protected freemarker.template.Configuration

Returns:

the FreeMarker configuration (never null)
obtainConfiguration
()

Obtain the FreeMarker configuration for actual use.

protected void
processTemplate(freemarker.template.Template
the template to process
template
,
freemarker.template.SimpleHash
the model for the template
model
,
HttpServletResponse
servlet response (use this to get the OutputStream or Writer)
response
)

Process the FreeMarker template to the servlet response.

protected void
renderMergedTemplateModel(Map<String, Object>
combined output Map, with request attributes and session attributes merged into it if required
model
,
HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
)

Implements abstract org.springframework.web.servlet.view.AbstractTemplateView.renderMergedTemplateModel.

Process the model map by merging it with the FreeMarker template.

public void
setConfiguration(freemarker.template.Configuration configuration)

Set the FreeMarker Configuration to be used by this view.

public void
setEncoding(String encoding)

Set the encoding of the FreeMarker template file.

Inherited from org.springframework.web.servlet.view.AbstractTemplateView:
applyContentTyperenderMergedOutputModelsetAllowRequestOverridesetAllowSessionOverridesetExposeRequestAttributessetExposeSessionAttributessetExposeSpringMacroHelpers

Field Detail

configurationback to summary
private freemarker.template.Configuration configuration
Annotations
@Nullable
encodingback to summary
private String encoding
Annotations
@Nullable

Constructor Detail

FreeMarkerViewback to summary
public FreeMarkerView()

Method Detail

autodetectConfigurationback to summary
protected FreeMarkerConfig autodetectConfiguration() throws BeansException

Autodetect a FreeMarkerConfig object via the ApplicationContext.

Returns:FreeMarkerConfig

the Configuration instance to use for FreeMarkerViews

Exceptions
BeansException:
if no Configuration instance could be found
See Also
getApplicationContext, setConfiguration
buildTemplateModelback to summary
protected freemarker.template.SimpleHash buildTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)

Build a FreeMarker template model for the given model Map.

The default implementation builds a SimpleHash for the given MVC model with an additional fallback to request attributes.

Parameters
model:Map<String, Object>

the model to use for rendering

request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current servlet response

Returns:freemarker.template.SimpleHash

the FreeMarker template model, as a SimpleHash or subclass thereof

checkResourceback to summary
public boolean checkResource(Locale locale) throws Exception

Overrides org.springframework.web.servlet.view.AbstractUrlBasedView.checkResource.

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.

Parameters
locale:Locale

Doc from org.springframework.web.servlet.view.AbstractUrlBasedView.checkResource.

the desired Locale that we're looking for

Returns:boolean

Doc from org.springframework.web.servlet.view.AbstractUrlBasedView.checkResource.

true if the resource exists (or is assumed to exist); false if we know that it does not exist

Annotations
@Override
Exceptions
Exception:

Doc from org.springframework.web.servlet.view.AbstractUrlBasedView.checkResource.

if the resource exists but is invalid (e.g. could not be parsed)

doRenderback to summary
protected void doRender(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception

Render the FreeMarker view to the given response, using the given model map which contains the complete template model to use.

The default implementation renders the template specified by the "url" bean property, retrieved via getTemplate. It delegates to the processTemplate method to merge the template instance with the given template model.

Can be overridden to customize the behavior, for example to render multiple templates into a single view.

Parameters
model:Map<String, Object>

the model to use for rendering

request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current servlet response

Exceptions
Exception:
if rendering failed
IOException:
if the template file could not be retrieved
See Also
setUrl, org.springframework.web.servlet.support.RequestContextUtils#getLocale, getTemplate(java.util.Locale), processTemplate, freemarker.ext.servlet.FreemarkerServlet
exposeHelpersback to summary
protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) throws Exception

Expose helpers unique to each rendering operation. This is necessary so that different rendering operations can't overwrite each other's formats etc.

Called by renderMergedTemplateModel. The default implementation is empty. This method can be overridden to add custom helpers to the model.

Parameters
model:Map<String, Object>

the model that will be passed to the template at merge time

request:HttpServletRequest

current HTTP request

Exceptions
Exception:
if there's a fatal error while we're adding information to the context
See Also
renderMergedTemplateModel
getConfigurationback to summary
protected freemarker.template.Configuration getConfiguration()

Return the FreeMarker configuration used by this view.

Annotations
@Nullable
getEncodingback to summary
protected String getEncoding()

Return the encoding for the FreeMarker template.

Annotations
@Nullable
getObjectWrapperback to summary
protected freemarker.template.ObjectWrapper getObjectWrapper()

Return the configured FreeMarker ObjectWrapper, or the default wrapper if none specified.

See Also
freemarker.template.Configuration#getObjectWrapper()
getTemplateback to summary
protected freemarker.template.Template getTemplate(Locale locale) throws IOException

Retrieve the FreeMarker template for the given locale, to be rendering by this view.

By default, the template specified by the "url" bean property will be retrieved.

Parameters
locale:Locale

the current locale

Returns:freemarker.template.Template

the FreeMarker template to render

Exceptions
IOException:
if the template file could not be retrieved
See Also
setUrl, getTemplate(String, java.util.Locale)
getTemplateback to summary
protected freemarker.template.Template getTemplate(String name, Locale locale) throws IOException

Retrieve the FreeMarker template specified by the given name, using the encoding specified by the "encoding" bean property.

Can be called by subclasses to retrieve a specific template, for example to render multiple templates into a single view.

Parameters
name:String

the file name of the desired template

locale:Locale

the current locale

Returns:freemarker.template.Template

the FreeMarker template

Exceptions
IOException:
if the template file could not be retrieved
initServletContextback to summary
protected void initServletContext(ServletContext servletContext) throws BeansException

Overrides org.springframework.web.context.support.WebApplicationObjectSupport.initServletContext.

Invoked on startup. Looks for a single FreeMarkerConfig bean to find the relevant Configuration for this factory.

Checks that the template for the default Locale can be found: FreeMarker will check non-Locale-specific templates if a locale-specific one is not found.

Parameters
servletContext:ServletContext

Doc from org.springframework.web.context.support.WebApplicationObjectSupport.initServletContext.

the ServletContext that this application object runs in (never null)

Annotations
@Override
See Also
freemarker.cache.TemplateCache#getTemplate
obtainConfigurationback to summary
protected freemarker.template.Configuration obtainConfiguration()

Obtain the FreeMarker configuration for actual use.

Returns:freemarker.template.Configuration

the FreeMarker configuration (never null)

Exceptions
IllegalStateException:
in case of no Configuration object set
Since
5.0
processTemplateback to summary
protected void processTemplate(freemarker.template.Template template, freemarker.template.SimpleHash model, HttpServletResponse response) throws IOException, freemarker.template.TemplateException

Process the FreeMarker template to the servlet response.

Can be overridden to customize the behavior.

Parameters
template:freemarker.template.Template

the template to process

model:freemarker.template.SimpleHash

the model for the template

response:HttpServletResponse

servlet response (use this to get the OutputStream or Writer)

Exceptions
IOException:
if the template file could not be retrieved
TemplateException:
if thrown by FreeMarker
See Also
freemarker.template.Template#process(Object, java.io.Writer)
renderMergedTemplateModelback to summary
protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception

Implements abstract org.springframework.web.servlet.view.AbstractTemplateView.renderMergedTemplateModel.

Process the model map by merging it with the FreeMarker template. Output is directed to the servlet response.

This method can be overridden if custom behavior is needed.

Parameters
model:Map<String, Object>

Doc from org.springframework.web.servlet.view.AbstractTemplateView.renderMergedTemplateModel.

combined output Map, with request attributes and session attributes merged into it if required

request:HttpServletRequest

Doc from org.springframework.web.servlet.view.AbstractTemplateView.renderMergedTemplateModel.

current HTTP request

response:HttpServletResponse

Doc from org.springframework.web.servlet.view.AbstractTemplateView.renderMergedTemplateModel.

current HTTP response

Annotations
@Override
Exceptions
Exception:

Doc from org.springframework.web.servlet.view.AbstractTemplateView.renderMergedTemplateModel.

if rendering failed

setConfigurationback to summary
public void setConfiguration(freemarker.template.Configuration configuration)

Set the FreeMarker Configuration to be used by this view.

If this is not set, the default lookup will occur: a single FreeMarkerConfig is expected in the current web application context, with any bean name.

setEncodingback to summary
public void setEncoding(String encoding)

Set the encoding of the FreeMarker template file. Default is determined by the FreeMarker Configuration: "ISO-8859-1" if not specified otherwise.

Specify the encoding in the FreeMarker Configuration rather than per template if all your templates share a common encoding.

org.springframework.web.servlet.view.freemarker back to summary

private Class FreeMarkerView.RequestHashModel

extends freemarker.template.SimpleHash
Class Inheritance
  • java.lang.Object
  • ⋱
  • freemarker.template.SimpleHash
  • org.springframework.web.servlet.view.freemarker.FreeMarkerView.RequestHashModel
Annotations
@SuppressWarnings:serial

Extension of FreeMarker SimpleHash, adding a fallback to request attributes. Similar to the formerly used freemarker.ext.servlet.AllHttpScopesHashModel, just limited to common request attribute exposure.

Field Summary

Modifier and TypeField and Description
private final HttpServletRequest

Constructor Summary

AccessConstructor and Description
public
RequestHashModel(freemarker.template.ObjectWrapper wrapper, HttpServletRequest request)

Method Summary

Modifier and TypeMethod and Description
public freemarker.template.TemplateModel
get(String key)

Field Detail

requestback to summary
private final HttpServletRequest request

Constructor Detail

RequestHashModelback to summary
public RequestHashModel(freemarker.template.ObjectWrapper wrapper, HttpServletRequest request)

Method Detail

getback to summary
public freemarker.template.TemplateModel get(String key) throws freemarker.template.TemplateModelException
Annotations
@Override