类 FreeMarkerView
- 所有已实现的接口:
cn.taketoday.beans.factory.Aware,cn.taketoday.beans.factory.BeanNameAware,cn.taketoday.beans.factory.InitializingBean,cn.taketoday.context.ApplicationContextAware,View
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 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(freemarker.template.Configuration) for more details on the impacts
of this approach.
Note: Infra FreeMarker support requires FreeMarker 2.3 or higher. 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.
- 从以下版本开始:
- 4.0
- 作者:
- Darren Davison, Juergen Hoeller, Harry Yang
- 另请参阅:
-
嵌套类概要
嵌套类修饰符和类型类说明private static classExtension of FreeMarkerSimpleHash, adding a fallback to request attributes. -
字段概要
字段从类继承的字段 cn.taketoday.web.view.AbstractView
DEFAULT_CONTENT_TYPE从类继承的字段 cn.taketoday.context.support.ApplicationObjectSupport
applicationContext, log, messageSourceAccessor从接口继承的字段 cn.taketoday.web.view.View
RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明protected FreeMarkerConfigAutodetect aFreeMarkerConfigobject via the ApplicationContext.protected freemarker.template.SimpleHashbuildTemplateModel(Map<String, Object> model, RequestContext request) Build a FreeMarker template model for the given model Map.booleancheckResource(Locale locale) Check that the FreeMarker template used for this view exists and is valid.protected voiddoRender(Map<String, Object> model, RequestContext context) Render the FreeMarker view to the given response, using the given model map which contains the complete template model to use.protected voidexposeHelpers(Map<String, Object> model, RequestContext request) Expose helpers unique to each rendering operation.protected freemarker.template.ConfigurationReturn the FreeMarker configuration used by this view.protected StringReturn the encoding for the FreeMarker template.protected freemarker.template.ObjectWrapperReturn the configured FreeMarkerObjectWrapper, or thedefault wrapperif none specified.protected freemarker.template.TemplategetTemplate(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.TemplategetTemplate(Locale locale) Retrieve the FreeMarker template for the given locale, to be rendering by this view.protected voidInvoked on startup.protected freemarker.template.ConfigurationObtain the FreeMarker configuration for actual use.protected voidprocessTemplate(freemarker.template.Template template, freemarker.template.SimpleHash model, RequestContext response) Process the FreeMarker template to the servlet response.protected voidrenderMergedTemplateModel(Map<String, Object> model, RequestContext context) Process the model map by merging it with the FreeMarker template.voidsetConfiguration(freemarker.template.Configuration configuration) Set the FreeMarker Configuration to be used by this view.voidsetEncoding(String encoding) Set the encoding of the FreeMarker template file.从类继承的方法 cn.taketoday.web.view.AbstractTemplateView
applyContentType, renderMergedOutputModel, setAllowRequestOverride, setAllowSessionOverride, setExposeRequestAttributes, setExposeSessionAttributes从类继承的方法 cn.taketoday.web.view.AbstractUrlBasedView
afterPropertiesSet, getUrl, isUrlRequired, setUrl, toString从类继承的方法 cn.taketoday.web.view.AbstractView
addStaticAttribute, createMergedOutputModel, createTemporaryOutputStream, exposeModelAsRequestAttributes, formatViewName, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getRequestContextToExpose, getStaticAttributes, isExposePathVariables, prepareResponse, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposeOutputRedirectModel, setExposePathVariables, setRequestContextAttribute, setResponseContentType, writeToResponse从类继承的方法 cn.taketoday.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, initApplicationContext, isContextRequired, obtainApplicationContext, requiredContextClass, setApplicationContext, unwrapContext, unwrapFactory
-
字段详细资料
-
encoding
-
configuration
@Nullable private freemarker.template.Configuration configuration
-
-
构造器详细资料
-
FreeMarkerView
public FreeMarkerView()
-
-
方法详细资料
-
setEncoding
Set the encoding of the FreeMarker template file. Default is determined by the FreeMarker Configuration: "UTF-8" if not specified otherwise.Specify the encoding in the FreeMarker Configuration rather than per template if all your templates share a common encoding.
-
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
FreeMarkerConfigis expected in the current web application context, with any bean name. -
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
-
initApplicationContext
protected void initApplicationContext()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.
- 覆盖:
initApplicationContext在类中cn.taketoday.context.support.ApplicationObjectSupport- 另请参阅:
-
TemplateCache.getTemplate(java.lang.String, java.util.Locale, java.lang.Object, java.lang.String, boolean)
-
autodetectConfiguration
Autodetect aFreeMarkerConfigobject via the ApplicationContext.- 返回:
- the Configuration instance to use for FreeMarkerViews
- 抛出:
cn.taketoday.beans.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 wrapperif none specified.- 另请参阅:
-
Configurable.getObjectWrapper()
-
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.
- 覆盖:
checkResource在类中AbstractUrlBasedView- 参数:
locale- the desired Locale that we're looking for- 返回:
trueif the resource exists (or is assumed to exist);falseif 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, RequestContext context) throws ExceptionProcess 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 requiredcontext- current HTTP request- 抛出:
Exception- if rendering failed
-
exposeHelpers
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- 另请参阅:
-
doRender
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 theprocessTemplatemethod 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 renderingcontext- current HTTP request context- 抛出:
IOException- if the template file could not be retrievedException- if rendering failed- 另请参阅:
-
buildTemplateModel
protected freemarker.template.SimpleHash buildTemplateModel(Map<String, Object> model, RequestContext request) Build a FreeMarker template model for the given model Map.The default implementation builds a
SimpleHashfor the given MVC model with an additional fallback to request attributes.- 参数:
model- the model to use for renderingrequest- current HTTP request- 返回:
- the FreeMarker template model, as a
SimpleHashor subclass thereof
-
getTemplate
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- 另请参阅:
-
getTemplate
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, RequestContext 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)
-