类 VelocityView
- 所有已实现的接口:
org.springframework.beans.factory.Aware,org.springframework.beans.factory.BeanNameAware,org.springframework.beans.factory.InitializingBean,org.springframework.context.ApplicationContextAware,org.springframework.web.context.ServletContextAware,org.springframework.web.servlet.View
- 直接已知子类:
VelocityToolboxView
Exposes the following JavaBean properties:
- url: the location of the Velocity template to be wrapped, relative to the Velocity resource loader path (see VelocityConfigurer).
- encoding (optional, default is determined by Velocity configuration): the encoding of the Velocity template file
- velocityFormatterAttribute (optional, default=null): the name of
the VelocityFormatter helper object to expose in the Velocity context of this
view, or
nullif not needed. VelocityFormatter is part of standard Velocity. - dateToolAttribute (optional, default=null): the name of the
DateTool helper object to expose in the Velocity context of this view,
or
nullif not needed. DateTool is part of Velocity Tools. - numberToolAttribute (optional, default=null): the name of the
NumberTool helper object to expose in the Velocity context of this view,
or
nullif not needed. NumberTool is part of Velocity Tools. - cacheTemplate (optional, default=false): whether or not the Velocity template should be cached. It should normally be true in production, but setting this to false enables us to modify Velocity templates without restarting the application (similar to JSPs). Note that this is a minor optimization only, as Velocity itself caches templates in a modification-aware fashion.
Depends on a VelocityConfig object such as VelocityConfigurer being accessible in the current web application context, with any bean name. Alternatively, you can set the VelocityEngine object as bean property.
Note: Spring 3.0's VelocityView requires Velocity 1.4 or higher, and optionally Velocity Tools 1.1 or higher (depending on the use of DateTool and/or NumberTool).
- 作者:
- Rod Johnson, Juergen Hoeller, Dave Syer
- 另请参阅:
-
VelocityConfigVelocityConfigurerAbstractUrlBasedView.setUrl(java.lang.String)AbstractTemplateView.setExposeSpringMacroHelpers(boolean)setEncoding(java.lang.String)setVelocityEngine(org.apache.velocity.app.VelocityEngine)VelocityConfigVelocityConfigurer
-
字段概要
从类继承的字段 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 -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明protected org.apache.velocity.app.VelocityEngineAutodetect a VelocityEngine via the ApplicationContext.booleancheckResource(Locale locale) Check that the Velocity template used for this view exists and is valid.protected org.apache.velocity.context.ContextcreateVelocityContext(Map<String, Object> model) Create a Velocity Context instance for the given model, to be passed to the template for merging.protected org.apache.velocity.context.ContextcreateVelocityContext(Map<String, Object> model, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Create a Velocity Context instance for the given model, to be passed to the template for merging.protected voiddoRender(org.apache.velocity.context.Context context, jakarta.servlet.http.HttpServletResponse response) Render the Velocity view to the given response, using the given Velocity context which contains the complete template model to use.protected voidexposeHelpers(Map<String, Object> model, jakarta.servlet.http.HttpServletRequest request) Expose helpers unique to each rendering operation.protected voidexposeHelpers(org.apache.velocity.context.Context velocityContext, jakarta.servlet.http.HttpServletRequest request) Expose helpers unique to each rendering operation.protected voidexposeHelpers(org.apache.velocity.context.Context velocityContext, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Expose helpers unique to each rendering operation.protected voidexposeToolAttributes(org.apache.velocity.context.Context velocityContext, jakarta.servlet.http.HttpServletRequest request) Expose the tool attributes, according to corresponding bean property settings.protected StringReturn the encoding for the Velocity template.protected org.apache.velocity.TemplateRetrieve the Velocity template to be rendered by this view.protected org.apache.velocity.TemplategetTemplate(String name) Retrieve the Velocity template specified by the given name, using the encoding specified by the "encoding" bean property.protected org.apache.velocity.app.VelocityEngineReturn the VelocityEngine used by this view.protected voidInvoked on startup.protected voidInitialize the given tool instance.protected booleanprotected voidmergeTemplate(org.apache.velocity.Template template, org.apache.velocity.context.Context context, jakarta.servlet.http.HttpServletResponse response) Merge the template with the context.protected voidrenderMergedTemplateModel(Map<String, Object> model, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Process the model map by merging it with the Velocity template.voidsetCacheTemplate(boolean cacheTemplate) Set whether the Velocity template should be cached.voidsetDateToolAttribute(String dateToolAttribute) Set the name of the DateTool helper object to expose in the Velocity context of this view, ornullif not needed.voidsetEncoding(String encoding) Set the encoding of the Velocity template file.voidsetNumberToolAttribute(String numberToolAttribute) Set the name of the NumberTool helper object to expose in the Velocity context of this view, ornullif not needed.voidsetToolAttributes(Map<String, Class<?>> toolAttributes) Set tool attributes to expose to the view, as attribute name / class name pairs.voidsetVelocityEngine(org.apache.velocity.app.VelocityEngine velocityEngine) Set the VelocityEngine to be used by this view.从类继承的方法 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, initServletContext, isContextRequired, setServletContext从类继承的方法 org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, obtainApplicationContext, requiredContextClass, setApplicationContext
-
构造器详细资料
-
VelocityView
public VelocityView()
-
-
方法详细资料
-
setToolAttributes
Set tool attributes to expose to the view, as attribute name / class name pairs. An instance of the given class will be added to the Velocity context for each rendering operation, under the given attribute name.For example, an instance of MathTool, which is part of the generic package of Velocity Tools, can be bound under the attribute name "math", specifying the fully qualified class name "org.apache.velocity.tools.generic.MathTool" as value.
Note that VelocityView can only create simple generic tools or values, that is, classes with a public default constructor and no further initialization needs. This class does not do any further checks, to not introduce a required dependency on a specific tools package.
For tools that are part of the view package of Velocity Tools, a special Velocity context and a special init callback are needed. Use VelocityToolboxView in such a case, or override
createVelocityContextandinitToolaccordingly.For a simple VelocityFormatter instance or special locale-aware instances of DateTool/NumberTool, which are part of the generic package of Velocity Tools, specify the "velocityFormatterAttribute", "dateToolAttribute" or "numberToolAttribute" properties, respectively.
- 参数:
toolAttributes- attribute names as keys, and tool class names as values- 另请参阅:
-
MathToolVelocityToolboxViewcreateVelocityContext(java.util.Map<java.lang.String, java.lang.Object>, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)initTool(java.lang.Object, org.apache.velocity.context.Context)setDateToolAttribute(java.lang.String)setNumberToolAttribute(java.lang.String)
-
setDateToolAttribute
Set the name of the DateTool helper object to expose in the Velocity context of this view, ornullif not needed. The exposed DateTool will be aware of the current locale, as determined by Spring's LocaleResolver.DateTool is part of the generic package of Velocity Tools 1.0. Spring uses a special locale-aware subclass of DateTool.
- 参数:
dateToolAttribute- dateToolAttribute- 另请参阅:
-
DateToolRequestContextUtils.getLocale(jakarta.servlet.http.HttpServletRequest)LocaleResolver
-
setNumberToolAttribute
Set the name of the NumberTool helper object to expose in the Velocity context of this view, ornullif not needed. The exposed NumberTool will be aware of the current locale, as determined by Spring's LocaleResolver.NumberTool is part of the generic package of Velocity Tools 1.1. Spring uses a special locale-aware subclass of NumberTool.
- 参数:
numberToolAttribute- numberToolAttribute- 另请参阅:
-
NumberToolRequestContextUtils.getLocale(jakarta.servlet.http.HttpServletRequest)LocaleResolver
-
setEncoding
Set the encoding of the Velocity template file. Default is determined by the VelocityEngine: "ISO-8859-1" if not specified otherwise.Specify the encoding in the VelocityEngine rather than per template if all your templates share a common encoding.
- 参数:
encoding- encoding
-
getEncoding
Return the encoding for the Velocity template.- 返回:
- encoding
-
setCacheTemplate
public void setCacheTemplate(boolean cacheTemplate) Set whether the Velocity template should be cached. Default is "false". It should normally be true in production, but setting this to false enables us to modify Velocity templates without restarting the application (similar to JSPs).Note that this is a minor optimization only, as Velocity itself caches templates in a modification-aware fashion.
- 参数:
cacheTemplate- cacheTemplate
-
isCacheTemplate
protected boolean isCacheTemplate()- 返回:
- Return whether the Velocity template should be cached.
-
setVelocityEngine
public void setVelocityEngine(org.apache.velocity.app.VelocityEngine velocityEngine) Set the VelocityEngine to be used by this view.If this is not set, the default lookup will occur: A single VelocityConfig is expected in the current web application context, with any bean name.
- 参数:
velocityEngine- velocityEngine- 另请参阅:
-
getVelocityEngine
protected org.apache.velocity.app.VelocityEngine getVelocityEngine()Return the VelocityEngine used by this view.- 返回:
- velocityEngine
-
initApplicationContext
protected void initApplicationContext() throws org.springframework.beans.BeansExceptionInvoked on startup. Looks for a single VelocityConfig bean to find the relevant VelocityEngine for this factory.- 覆盖:
initApplicationContext在类中org.springframework.context.support.ApplicationObjectSupport- 抛出:
org.springframework.beans.BeansException
-
autodetectVelocityEngine
protected org.apache.velocity.app.VelocityEngine autodetectVelocityEngine() throws org.springframework.beans.BeansExceptionAutodetect a VelocityEngine via the ApplicationContext. Called if no explicit VelocityEngine has been specified.- 返回:
- the VelocityEngine to use for VelocityViews
- 抛出:
org.springframework.beans.BeansException- if no VelocityEngine could be found- 另请参阅:
-
ApplicationObjectSupport.getApplicationContext()setVelocityEngine(org.apache.velocity.app.VelocityEngine)
-
checkResource
Check that the Velocity 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在类中org.springframework.web.servlet.view.AbstractUrlBasedView- 抛出:
Exception
-
renderMergedTemplateModel
protected void renderMergedTemplateModel(Map<String, Object> model, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws ExceptionProcess the model map by merging it with the Velocity template. Output is directed to the servlet response.This method can be overridden if custom behavior is needed.
- 指定者:
renderMergedTemplateModel在类中org.springframework.web.servlet.view.AbstractTemplateView- 抛出:
Exception
-
exposeHelpers
protected void exposeHelpers(Map<String, Object> model, jakarta.servlet.http.HttpServletRequest request) throws ExceptionExpose 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 for mergingrequest- current HTTP request- 抛出:
Exception- if there's a fatal error while we're adding model attributes- 另请参阅:
-
createVelocityContext
protected org.apache.velocity.context.Context createVelocityContext(Map<String, Object> model, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws ExceptionCreate a Velocity Context instance for the given model, to be passed to the template for merging.The default implementation delegates to
createVelocityContext(Map). Can be overridden for a special context class, for example ChainedContext which is part of the view package of Velocity Tools. ChainedContext is needed for initialization of ViewTool instances.Have a look at
VelocityToolboxView, which pre-implements ChainedContext support. This is not part of the standard VelocityView class in order to avoid a required dependency on the view package of Velocity Tools.- 参数:
model- the model Map, containing the model attributes to be exposed to the viewrequest- current HTTP requestresponse- current HTTP response- 返回:
- the Velocity Context
- 抛出:
Exception- if there's a fatal error while creating the context- 另请参阅:
-
createVelocityContext
protected org.apache.velocity.context.Context createVelocityContext(Map<String, Object> model) throws ExceptionCreate a Velocity Context instance for the given model, to be passed to the template for merging.Default implementation creates an instance of Velocity's VelocityContext implementation class.
- 参数:
model- the model Map, containing the model attributes to be exposed to the view- 返回:
- the Velocity Context
- 抛出:
Exception- if there's a fatal error while creating the context- 另请参阅:
-
VelocityContext
-
exposeHelpers
protected void exposeHelpers(org.apache.velocity.context.Context velocityContext, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) 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. Default implementation delegates toexposeHelpers(velocityContext, request). This method can be overridden to add special tools to the context, needing the servlet response to initialize (see Velocity Tools, for example LinkTool and ViewTool/ChainedContext).- 参数:
velocityContext- Velocity context that will be passed to the templaterequest- current HTTP requestresponse- current HTTP response- 抛出:
Exception- if there's a fatal error while we're adding model attributes- 另请参阅:
-
exposeHelpers
protected void exposeHelpers(org.apache.velocity.context.Context velocityContext, jakarta.servlet.http.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.Default implementation is empty. This method can be overridden to add custom helpers to the Velocity context.
- 参数:
velocityContext- Velocity context that will be passed to the templaterequest- current HTTP request- 抛出:
Exception- if there's a fatal error while we're adding model attributes- 另请参阅:
-
exposeToolAttributes
protected void exposeToolAttributes(org.apache.velocity.context.Context velocityContext, jakarta.servlet.http.HttpServletRequest request) throws Exception Expose the tool attributes, according to corresponding bean property settings.Do not override this method unless for further tools driven by bean properties. Override one of the
exposeHelpersmethods to add custom helpers.- 参数:
velocityContext- Velocity context that will be passed to the templaterequest- current HTTP request- 抛出:
Exception- if there's a fatal error while we're adding model attributes- 另请参阅:
-
initTool
protected void initTool(Object tool, org.apache.velocity.context.Context velocityContext) throws Exception Initialize the given tool instance. The default implementation is empty.Can be overridden to check for special callback interfaces, for example the ViewContext interface which is part of the view package of Velocity Tools. In the particular case of ViewContext, you'll usually also need a special Velocity context, like ChainedContext which is part of Velocity Tools too.
Have a look at
VelocityToolboxView, which pre-implements such a ViewTool check. This is not part of the standard VelocityView class in order to avoid a required dependency on the view package of Velocity Tools.- 参数:
tool- the tool instance to initializevelocityContext- the Velocity context- 抛出:
Exception- if initializion of the tool failed- 另请参阅:
-
doRender
protected void doRender(org.apache.velocity.context.Context context, jakarta.servlet.http.HttpServletResponse response) throws Exception Render the Velocity view to the given response, using the given Velocity context 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 themergeTemplatemethod to merge the template instance with the given Velocity context.Can be overridden to customize the behavior, for example to render multiple templates into a single view.
- 参数:
context- the Velocity context to use for renderingresponse- servlet response (use this to get the OutputStream or Writer)- 抛出:
Exception- if thrown by Velocity- 另请参阅:
-
AbstractUrlBasedView.setUrl(java.lang.String)getTemplate()mergeTemplate(org.apache.velocity.Template, org.apache.velocity.context.Context, jakarta.servlet.http.HttpServletResponse)
-
getTemplate
Retrieve the Velocity template to be rendered by this view.By default, the template specified by the "url" bean property will be retrieved: either returning a cached template instance or loading a fresh instance (according to the "cacheTemplate" bean property)
- 返回:
- the Velocity template to render
- 抛出:
Exception- if thrown by Velocity- 另请参阅:
-
AbstractUrlBasedView.setUrl(java.lang.String)setCacheTemplate(boolean)getTemplate(String)
-
getTemplate
Retrieve the Velocity 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 template- 返回:
- the Velocity template
- 抛出:
Exception- if thrown by Velocity- 另请参阅:
-
VelocityEngine.getTemplate(java.lang.String)
-
mergeTemplate
protected void mergeTemplate(org.apache.velocity.Template template, org.apache.velocity.context.Context context, jakarta.servlet.http.HttpServletResponse response) throws Exception Merge the template with the context. Can be overridden to customize the behavior.- 参数:
template- the template to mergecontext- the Velocity context to use for renderingresponse- servlet response (use this to get the OutputStream or Writer)- 抛出:
Exception- if thrown by Velocity- 另请参阅:
-
Template.merge(org.apache.velocity.context.Context, java.io.Writer)
-