org.springframework.web.servlet.view.velocity
Class VelocityView

java.lang.Object
  extended byorg.springframework.context.support.ApplicationObjectSupport
      extended byorg.springframework.web.context.support.WebApplicationObjectSupport
          extended byorg.springframework.web.servlet.view.AbstractView
              extended byorg.springframework.web.servlet.view.AbstractUrlBasedView
                  extended byorg.springframework.web.servlet.view.AbstractTemplateView
                      extended byorg.springframework.web.servlet.view.velocity.VelocityView
All Implemented Interfaces:
ApplicationContextAware, BeanNameAware, View

public class VelocityView
extends AbstractTemplateView

View using the Velocity template engine. Based on code in the VelocityServlet shipped with Velocity.

Exposes the following JavaBean properties:

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.

Author:
Rod Johnson, Juergen Hoeller
See Also:
VelocityConfig, VelocityConfigurer, AbstractUrlBasedView.setUrl(java.lang.String), AbstractTemplateView.setExposeSpringMacroHelpers(boolean), setEncoding(java.lang.String), setVelocityEngine(org.apache.velocity.app.VelocityEngine), VelocityConfig, VelocityConfigurer

Field Summary
 
Fields inherited from class org.springframework.web.servlet.view.AbstractTemplateView
SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE
 
Fields inherited from class org.springframework.web.servlet.view.AbstractView
DEFAULT_CONTENT_TYPE
 
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
 
Constructor Summary
VelocityView()
           
 
Method Summary
protected  void exposeHelpers(org.apache.velocity.context.Context velocityContext, javax.servlet.http.HttpServletRequest request)
          Deprecated. in favor of exposeHelpers(Map, HttpServletRequest)
protected  void exposeHelpers(java.util.Map model, javax.servlet.http.HttpServletRequest request)
          Expose helpers unique to each rendering operation.
protected  java.lang.String getEncoding()
          Return the encoding for the Velocity template.
protected  org.apache.velocity.Template getTemplate()
          Retrieve the Velocity template.
protected  org.apache.velocity.app.VelocityEngine getVelocityEngine()
          Return the VelocityEngine used by this view.
protected  void initApplicationContext()
          Invoked on startup.
protected  void mergeTemplate(org.apache.velocity.Template template, org.apache.velocity.context.Context context, javax.servlet.http.HttpServletResponse response)
          Merge the template with the context.
protected  void renderMergedTemplateModel(java.util.Map model, javax.servlet.http.HttpServletRequest request, javax.servlet.http.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(java.lang.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(java.lang.String encoding)
          Set the encoding of the Velocity template file.
 void setNumberToolAttribute(java.lang.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 setVelocityEngine(org.apache.velocity.app.VelocityEngine velocityEngine)
          Set the VelocityEngine to be used by this view.
 void setVelocityFormatterAttribute(java.lang.String velocityFormatterAttribute)
          Set the name of the VelocityFormatter helper object to expose in the Velocity context of this view, or null if not needed.
 
Methods inherited from class org.springframework.web.servlet.view.AbstractTemplateView
renderMergedOutputModel, setAllowRequestOverride, setAllowSessionOverride, setExposeRequestAttributes, setExposeSessionAttributes, setExposeSpringMacroHelpers
 
Methods inherited from class org.springframework.web.servlet.view.AbstractUrlBasedView
getUrl, setUrl
 
Methods inherited from class org.springframework.web.servlet.view.AbstractView
addStaticAttribute, createRequestContext, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getStaticAttributes, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setRequestContextAttribute
 
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext
 
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, requiredContextClass, setApplicationContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

VelocityView

public VelocityView()
Method Detail

setEncoding

public void setEncoding(java.lang.String encoding)
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.


getEncoding

protected java.lang.String getEncoding()
Return the encoding for the Velocity template.


setVelocityFormatterAttribute

public void setVelocityFormatterAttribute(java.lang.String velocityFormatterAttribute)
Set the name of the VelocityFormatter helper object to expose in the Velocity context of this view, or null if not needed. VelocityFormatter is part of the standard Velocity distribution.

See Also:
VelocityFormatter

setDateToolAttribute

public void setDateToolAttribute(java.lang.String dateToolAttribute)
Set the name of the DateTool helper object to expose in the Velocity context of this view, or null if not needed. DateTool is part of Velocity Tools 1.0.

See Also:
DateTool

setNumberToolAttribute

public void setNumberToolAttribute(java.lang.String numberToolAttribute)
Set the name of the NumberTool helper object to expose in the Velocity context of this view, or null if not needed. NumberTool is part of Velocity Tools 1.1.

See Also:
NumberTool

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.


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.

See Also:
VelocityConfig

getVelocityEngine

protected org.apache.velocity.app.VelocityEngine getVelocityEngine()
Return the VelocityEngine used by this view.


initApplicationContext

protected void initApplicationContext()
                               throws BeansException
Invoked on startup. Looks for a single VelocityConfig bean to find the relevant VelocityEngine for this factory.

Overrides:
initApplicationContext in class AbstractUrlBasedView
Throws:
BeansException

renderMergedTemplateModel

protected void renderMergedTemplateModel(java.util.Map model,
                                         javax.servlet.http.HttpServletRequest request,
                                         javax.servlet.http.HttpServletResponse response)
                                  throws java.lang.Exception
Process the model map by merging it with the Velocity template. Output is directed to the response. This method can be overridden if custom behavior is needed.

Specified by:
renderMergedTemplateModel in class AbstractTemplateView
Parameters:
model - combined output Map, with request attributes and session attributes merged into it if required
request - current HTTP request
response - current HTTP response
Throws:
java.lang.Exception - if rendering failed

getTemplate

protected org.apache.velocity.Template getTemplate()
                                            throws java.lang.Exception
Retrieve the Velocity template.

Returns:
the Velocity template to process
Throws:
java.lang.Exception - if thrown by Velocity

exposeHelpers

protected void exposeHelpers(java.util.Map model,
                             javax.servlet.http.HttpServletRequest request)
                      throws java.lang.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 - the model that will be passed to the template at merge time
request - current HTTP request
Throws:
java.lang.Exception - if there's a fatal error while we're adding model attributes
See Also:
renderMergedTemplateModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

exposeHelpers

protected void exposeHelpers(org.apache.velocity.context.Context velocityContext,
                             javax.servlet.http.HttpServletRequest request)
                      throws java.lang.Exception
Deprecated. in favor of exposeHelpers(Map, HttpServletRequest)

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 implementations is empty. This method can be overridden to add custom helpers to the Velocity context.

Parameters:
velocityContext - Velocity context that will be passed to the template at merge time
request - current HTTP request
Throws:
java.lang.Exception - if there's a fatal error while we're adding model attributes
See Also:
exposeHelpers(Map, HttpServletRequest)

mergeTemplate

protected void mergeTemplate(org.apache.velocity.Template template,
                             org.apache.velocity.context.Context context,
                             javax.servlet.http.HttpServletResponse response)
                      throws java.lang.Exception
Merge the template with the context. Can be overridden to customize the behavior.

Parameters:
template - the template to merge
context - the Velocity context
response - servlet response (use this to get the OutputStream or Writer)
Throws:
java.lang.Exception
See Also:
Template.merge(org.apache.velocity.context.Context, java.io.Writer)


Copyright (C) 2003-2004 The Spring Framework Project.