public class VelocityView extends AbstractTemplateView
Exposes the following JavaBean properties:
null
if not needed. VelocityFormatter is part of standard Velocity.
null
if not needed. DateTool is part of Velocity Tools.
null
if not needed. NumberTool is part of Velocity Tools.
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).
VelocityConfig
,
VelocityConfigurer
,
AbstractUrlBasedView.setUrl(java.lang.String)
,
AbstractTemplateView.setExposeSpringMacroHelpers(boolean)
,
setEncoding(java.lang.String)
,
setVelocityEngine(org.apache.velocity.app.VelocityEngine)
,
VelocityConfig
,
VelocityConfigurer
SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE
DEFAULT_CONTENT_TYPE
logger
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE
Constructor and Description |
---|
VelocityView() |
Modifier and Type | Method and Description |
---|---|
protected org.apache.velocity.app.VelocityEngine |
autodetectVelocityEngine()
Autodetect a VelocityEngine via the ApplicationContext.
|
boolean |
checkResource(Locale locale)
Check that the Velocity template used for this view exists and is valid.
|
protected org.apache.velocity.context.Context |
createVelocityContext(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.Context |
createVelocityContext(Map<String,Object> model,
HttpServletRequest request,
HttpServletResponse response)
Create a Velocity Context instance for the given model,
to be passed to the template for merging.
|
protected void |
doRender(org.apache.velocity.context.Context context,
HttpServletResponse response)
Render the Velocity view to the given response, using the given Velocity
context which contains the complete template model to use.
|
protected void |
exposeHelpers(org.apache.velocity.context.Context velocityContext,
HttpServletRequest request)
Expose helpers unique to each rendering operation.
|
protected void |
exposeHelpers(org.apache.velocity.context.Context velocityContext,
HttpServletRequest request,
HttpServletResponse response)
Expose helpers unique to each rendering operation.
|
protected void |
exposeHelpers(Map<String,Object> model,
HttpServletRequest request)
Expose helpers unique to each rendering operation.
|
protected void |
exposeToolAttributes(org.apache.velocity.context.Context velocityContext,
HttpServletRequest request)
Expose the tool attributes, according to corresponding bean property settings.
|
protected String |
getEncoding()
Return the encoding for the Velocity template.
|
protected org.apache.velocity.Template |
getTemplate()
Retrieve the Velocity template to be rendered by this view.
|
protected org.apache.velocity.Template |
getTemplate(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.VelocityEngine |
getVelocityEngine()
Return the VelocityEngine used by this view.
|
protected void |
initApplicationContext()
Invoked on startup.
|
protected void |
initTool(Object tool,
org.apache.velocity.context.Context velocityContext)
Initialize the given tool instance.
|
protected boolean |
isCacheTemplate()
Return whether the Velocity template should be cached.
|
protected void |
mergeTemplate(org.apache.velocity.Template template,
org.apache.velocity.context.Context context,
HttpServletResponse response)
Merge the template with the context.
|
protected void |
renderMergedTemplateModel(Map<String,Object> model,
HttpServletRequest request,
HttpServletResponse response)
Process the model map by merging it with the Velocity template.
|
void |
setCacheTemplate(boolean cacheTemplate)
Set whether the Velocity template should be cached.
|
void |
setDateToolAttribute(String dateToolAttribute)
Set the name of the DateTool helper object to expose in the Velocity context
of this view, or
null if not needed. |
void |
setEncoding(String encoding)
Set the encoding of the Velocity template file.
|
void |
setNumberToolAttribute(String numberToolAttribute)
Set the name of the NumberTool helper object to expose in the Velocity context
of this view, or
null if not needed. |
void |
setToolAttributes(Map<String,Class> toolAttributes)
Set tool attributes to expose to the view, as attribute name / class name pairs.
|
void |
setVelocityEngine(org.apache.velocity.app.VelocityEngine velocityEngine)
Set the VelocityEngine to be used by this view.
|
applyContentType, renderMergedOutputModel, setAllowRequestOverride, setAllowSessionOverride, setExposeRequestAttributes, setExposeSessionAttributes, setExposeSpringMacroHelpers
afterPropertiesSet, getUrl, isUrlRequired, setUrl, toString
addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getStaticAttributes, isExposePathVariables, prepareResponse, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposePathVariables, setRequestContextAttribute, setResponseContentType, writeToResponse
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, requiredContextClass, setApplicationContext
public void setToolAttributes(Map<String,Class> toolAttributes)
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 createVelocityContext
and
initTool
accordingly.
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 valuesMathTool
,
VelocityToolboxView
,
createVelocityContext(java.util.Map<java.lang.String, java.lang.Object>, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
,
initTool(java.lang.Object, org.apache.velocity.context.Context)
,
setDateToolAttribute(java.lang.String)
,
setNumberToolAttribute(java.lang.String)
public void setDateToolAttribute(String dateToolAttribute)
null
if 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.
DateTool
,
RequestContextUtils.getLocale(javax.servlet.http.HttpServletRequest)
,
LocaleResolver
public void setNumberToolAttribute(String numberToolAttribute)
null
if 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.
NumberTool
,
RequestContextUtils.getLocale(javax.servlet.http.HttpServletRequest)
,
LocaleResolver
public void setEncoding(String encoding)
Specify the encoding in the VelocityEngine rather than per template if all your templates share a common encoding.
protected String getEncoding()
public void setCacheTemplate(boolean cacheTemplate)
Note that this is a minor optimization only, as Velocity itself caches templates in a modification-aware fashion.
protected boolean isCacheTemplate()
public void setVelocityEngine(org.apache.velocity.app.VelocityEngine velocityEngine)
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.
VelocityConfig
protected org.apache.velocity.app.VelocityEngine getVelocityEngine()
protected void initApplicationContext() throws BeansException
initApplicationContext
in class ApplicationObjectSupport
ApplicationContextException
- in case of initialization errorsBeansException
- if thrown by ApplicationContext methodsApplicationObjectSupport.setApplicationContext(org.springframework.context.ApplicationContext)
protected org.apache.velocity.app.VelocityEngine autodetectVelocityEngine() throws BeansException
BeansException
- if no VelocityEngine could be foundApplicationObjectSupport.getApplicationContext()
,
setVelocityEngine(org.apache.velocity.app.VelocityEngine)
public boolean checkResource(Locale locale) throws Exception
Can be overridden to customize the behavior, for example in case of multiple templates to be rendered into a single view.
checkResource
in class AbstractUrlBasedView
locale
- the desired Locale that we're looking fortrue
if the resource exists (or is assumed to exist);
false
if we know that it does not existException
- if the resource exists but is invalid (e.g. could not be parsed)protected void renderMergedTemplateModel(Map<String,Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception
This method can be overridden if custom behavior is needed.
renderMergedTemplateModel
in class AbstractTemplateView
model
- combined output Map, with request attributes and
session attributes merged into it if requiredrequest
- current HTTP requestresponse
- current HTTP responseException
- if rendering failedprotected void exposeHelpers(Map<String,Object> model, HttpServletRequest request) throws Exception
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 requestException
- if there's a fatal error while we're adding model attributesrenderMergedTemplateModel(java.util.Map<java.lang.String, java.lang.Object>, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
protected org.apache.velocity.context.Context createVelocityContext(Map<String,Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception
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 responseException
- if there's a fatal error while creating the contextcreateVelocityContext(Map)
,
initTool(java.lang.Object, org.apache.velocity.context.Context)
,
ChainedContext
,
VelocityToolboxView
protected org.apache.velocity.context.Context createVelocityContext(Map<String,Object> model) throws Exception
Default implementation creates an instance of Velocity's VelocityContext implementation class.
model
- the model Map, containing the model attributes
to be exposed to the viewException
- if there's a fatal error while creating the contextVelocityContext
protected void exposeHelpers(org.apache.velocity.context.Context velocityContext, HttpServletRequest request, HttpServletResponse response) throws Exception
Called by renderMergedTemplateModel
. Default implementation
delegates to exposeHelpers(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 responseException
- if there's a fatal error while we're adding model attributesexposeHelpers(org.apache.velocity.context.Context, HttpServletRequest)
protected void exposeHelpers(org.apache.velocity.context.Context velocityContext, HttpServletRequest request) throws Exception
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 requestException
- if there's a fatal error while we're adding model attributesexposeHelpers(Map, HttpServletRequest)
protected void exposeToolAttributes(org.apache.velocity.context.Context velocityContext, HttpServletRequest request) throws Exception
Do not override this method unless for further tools driven by bean properties.
Override one of the exposeHelpers
methods to add custom helpers.
velocityContext
- Velocity context that will be passed to the templaterequest
- current HTTP requestException
- if there's a fatal error while we're adding model attributessetDateToolAttribute(java.lang.String)
,
setNumberToolAttribute(java.lang.String)
,
exposeHelpers(Map, HttpServletRequest)
,
exposeHelpers(org.apache.velocity.context.Context, HttpServletRequest, HttpServletResponse)
protected void initTool(Object tool, org.apache.velocity.context.Context velocityContext) throws Exception
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 contextException
- if initializion of the tool failedcreateVelocityContext(java.util.Map<java.lang.String, java.lang.Object>, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
,
ViewContext
,
ChainedContext
,
VelocityToolboxView
protected void doRender(org.apache.velocity.context.Context context, HttpServletResponse response) throws Exception
The default implementation renders the template specified by the "url"
bean property, retrieved via getTemplate
. It delegates to the
mergeTemplate
method 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 VelocityAbstractUrlBasedView.setUrl(java.lang.String)
,
getTemplate()
,
mergeTemplate(org.apache.velocity.Template, org.apache.velocity.context.Context, javax.servlet.http.HttpServletResponse)
protected org.apache.velocity.Template getTemplate() throws Exception
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)
Exception
- if thrown by VelocityAbstractUrlBasedView.setUrl(java.lang.String)
,
setCacheTemplate(boolean)
,
getTemplate(String)
protected org.apache.velocity.Template getTemplate(String name) throws Exception
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 templateException
- if thrown by VelocityVelocityEngine.getTemplate(java.lang.String)
protected void mergeTemplate(org.apache.velocity.Template template, org.apache.velocity.context.Context context, HttpServletResponse response) throws Exception
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 VelocityTemplate.merge(org.apache.velocity.context.Context, java.io.Writer)