类 FreeMarkerView
- java.lang.Object
- org.springframework.context.support.ApplicationObjectSupport
- org.springframework.web.context.support.WebApplicationObjectSupport
- org.springframework.web.servlet.view.AbstractView
- org.springframework.web.servlet.view.AbstractUrlBasedView
- org.springframework.web.servlet.view.AbstractTemplateView
- org.springframework.web.servlet.view.freemarker.FreeMarkerView
public class FreeMarkerView extends AbstractTemplateView
View using the FreeMarker template engine.Exposes the following JavaBean properties:
- url: the location of the FreeMarker template to be wrapped, relative to the FreeMarker template context (directory).
- encoding (optional, default is determined by FreeMarker configuration): the encoding of the FreeMarker template file
Depends on a single
FreeMarkerConfig
object such asFreeMarkerConfigurer
being accessible in the current web application context, with any bean name. Alternatively, you can set the FreeMarkerConfiguration
object as a bean property. SeesetConfiguration(freemarker.template.Configuration)
for more details on the impacts of this approach.Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher.
- 从以下版本开始:
- 03.03.2004
- 作者:
- Darren Davison, Juergen Hoeller
- 另请参阅:
AbstractUrlBasedView.setUrl(java.lang.String)
,AbstractTemplateView.setExposeSpringMacroHelpers(boolean)
,setEncoding(java.lang.String)
,setConfiguration(freemarker.template.Configuration)
,FreeMarkerConfig
,FreeMarkerConfigurer
字段概要
从类继承的字段 org.springframework.web.servlet.view.AbstractTemplateView
SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE
从类继承的字段 org.springframework.web.servlet.view.AbstractView
DEFAULT_CONTENT_TYPE
从类继承的字段 org.springframework.context.support.ApplicationObjectSupport
logger
从接口继承的字段 org.springframework.web.servlet.View
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE
构造器概要
构造器 构造器 说明 FreeMarkerView()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected FreeMarkerConfig
autodetectConfiguration()
Autodetect aFreeMarkerConfig
object via the ApplicationContext.protected freemarker.template.SimpleHash
buildTemplateModel(Map<String,Object> model, HttpServletRequest request, HttpServletResponse response)
Build a FreeMarker template model for the given model Map.boolean
checkResource(Locale locale)
Check that the FreeMarker template used for this view exists and is valid.protected void
doRender(Map<String,Object> model, HttpServletRequest request, HttpServletResponse 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> model, HttpServletRequest 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 FreeMarkerObjectWrapper
, or thedefault wrapper
if none specified.protected freemarker.template.Template
getTemplate(String name, Locale locale)
Retrieve the FreeMarker template specified by the given name, using the encoding specified by the "encoding" bean property.protected freemarker.template.Template
getTemplate(Locale locale)
Retrieve the FreeMarker template for the given locale, to be rendering by this view.protected void
initServletContext(ServletContext servletContext)
Invoked on startup.protected freemarker.template.Configuration
obtainConfiguration()
Obtain the FreeMarker configuration for actual use.protected void
processTemplate(freemarker.template.Template template, freemarker.template.SimpleHash model, HttpServletResponse response)
Process the FreeMarker template to the servlet response.protected void
renderMergedTemplateModel(Map<String,Object> model, HttpServletRequest request, HttpServletResponse response)
Process the model map by merging it with the FreeMarker template.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.从类继承的方法 org.springframework.web.servlet.view.AbstractTemplateView
applyContentType, renderMergedOutputModel, setAllowRequestOverride, setAllowSessionOverride, setExposeRequestAttributes, setExposeSessionAttributes, setExposeSpringMacroHelpers
从类继承的方法 org.springframework.web.servlet.view.AbstractUrlBasedView
afterPropertiesSet, getUrl, isUrlRequired, setUrl, toString
从类继承的方法 org.springframework.web.servlet.view.AbstractView
addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, formatViewName, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getRequestToExpose, getStaticAttributes, isExposePathVariables, prepareResponse, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposePathVariables, setRequestContextAttribute, setResponseContentType, writeToResponse
从类继承的方法 org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, isContextRequired, setServletContext
从类继承的方法 org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext
构造器详细资料
FreeMarkerView
public FreeMarkerView()
方法详细资料
setEncoding
public void setEncoding(@Nullable 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.
getEncoding
@Nullable protected String getEncoding()
Return the encoding for the FreeMarker template.
setConfiguration
public void setConfiguration(@Nullable 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. Note: using this method will cause a new instance ofTaglibFactory
to created for every singleFreeMarkerView
instance. This can be quite expensive in terms of memory and initial CPU usage. In production it is recommended that you use aFreeMarkerConfig
which exposes a single sharedTaglibFactory
.
getConfiguration
@Nullable protected freemarker.template.Configuration getConfiguration()
Return the FreeMarker configuration used by this view.
obtainConfiguration
protected freemarker.template.Configuration obtainConfiguration()
Obtain the FreeMarker configuration for actual use.- 返回:
- the FreeMarker configuration (never
null
) - 抛出:
IllegalStateException
- in case of no Configuration object set- 从以下版本开始:
- 5.0
initServletContext
protected void initServletContext(ServletContext servletContext) throws BeansException
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.
- 覆盖:
initServletContext
在类中WebApplicationObjectSupport
- 参数:
servletContext
- the ServletContext that this application object runs in (nevernull
)- 抛出:
BeansException
- 另请参阅:
TemplateCache.getTemplate(java.lang.String, java.util.Locale, java.lang.Object, java.lang.String, boolean)
autodetectConfiguration
protected FreeMarkerConfig autodetectConfiguration() throws BeansException
Autodetect aFreeMarkerConfig
object via the ApplicationContext.- 返回:
- the Configuration instance to use for FreeMarkerViews
- 抛出:
BeansException
- if no Configuration instance could be found- 另请参阅:
ApplicationObjectSupport.getApplicationContext()
,setConfiguration(freemarker.template.Configuration)
getObjectWrapper
protected freemarker.template.ObjectWrapper getObjectWrapper()
Return the configured FreeMarkerObjectWrapper
, or thedefault wrapper
if none specified.- 另请参阅:
Configurable.getObjectWrapper()
checkResource
public boolean checkResource(Locale locale) throws Exception
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.
- 覆盖:
checkResource
在类中AbstractUrlBasedView
- 参数:
locale
- the desired Locale that we're looking for- 返回:
true
if the resource exists (or is assumed to exist);false
if we know that it does not exist- 抛出:
Exception
- if the resource exists but is invalid (e.g. could not be parsed)
renderMergedTemplateModel
protected void renderMergedTemplateModel(Map<String,Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception
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.
- 指定者:
renderMergedTemplateModel
在类中AbstractTemplateView
- 参数:
model
- combined output Map, with request attributes and session attributes merged into it if requiredrequest
- current HTTP requestresponse
- current HTTP response- 抛出:
Exception
- if rendering failed
exposeHelpers
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.- 参数:
model
- the model that will be passed to the template at merge timerequest
- current HTTP request- 抛出:
Exception
- if there's a fatal error while we're adding information to the context- 另请参阅:
renderMergedTemplateModel(java.util.Map<java.lang.String, java.lang.Object>, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
doRender
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 theprocessTemplate
method to merge the template instance with the given template model.Adds the standard Freemarker hash models to the model: request parameters, request, session and application (ServletContext), as well as the JSP tag library hash model.
Can be overridden to customize the behavior, for example to render multiple templates into a single view.
- 参数:
model
- the model to use for renderingrequest
- current HTTP requestresponse
- current servlet response- 抛出:
IOException
- if the template file could not be retrievedException
- if rendering failed- 另请参阅:
AbstractUrlBasedView.setUrl(java.lang.String)
,RequestContextUtils.getLocale(javax.servlet.http.HttpServletRequest)
,getTemplate(java.util.Locale)
,processTemplate(freemarker.template.Template, freemarker.template.SimpleHash, javax.servlet.http.HttpServletResponse)
,FreemarkerServlet
buildTemplateModel
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
AllHttpScopesHashModel
.- 参数:
model
- the model to use for renderingrequest
- current HTTP requestresponse
- current servlet response- 返回:
- the FreeMarker template model, as a
SimpleHash
or subclass thereof
getTemplate
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.
- 参数:
locale
- the current locale- 返回:
- the FreeMarker template to render
- 抛出:
IOException
- if the template file could not be retrieved- 另请参阅:
AbstractUrlBasedView.setUrl(java.lang.String)
,getTemplate(String, java.util.Locale)
getTemplate
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.
- 参数:
name
- the file name of the desired templatelocale
- the current locale- 返回:
- the FreeMarker template
- 抛出:
IOException
- if the template file could not be retrieved
processTemplate
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.
- 参数:
template
- the template to processmodel
- the model for the templateresponse
- servlet response (use this to get the OutputStream or Writer)- 抛出:
IOException
- if the template file could not be retrievedfreemarker.template.TemplateException
- if thrown by FreeMarker- 另请参阅:
Template.process(Object, java.io.Writer)