Package org.apache.velocity.tools.view
Class VelocityView
- java.lang.Object
-
- org.apache.velocity.tools.ToolManager
-
- org.apache.velocity.tools.view.ViewToolManager
-
- org.apache.velocity.tools.view.VelocityView
-
public class VelocityView extends ViewToolManager
The class provides the following features:
- renders Velocity templates
- provides support for an auto-loaded, configurable toolbox
- provides transparent access to the servlet request attributes, servlet session attributes and servlet context attributes by auto-searching them
- logs to the logging facility of the servlet API
VelocityView supports the following configuration parameters in web.xml:
- org.apache.velocity.tools
- Path and name of the toolbox configuration file. The path must be relative to the web application root directory. If this parameter is not found, the servlet will check for a toolbox file at '/WEB-INF/tools.xml'.
- org.apache.velocity.properties
- Path and name of the Velocity configuration file. The path must be relative to the web application root directory. If this parameter is not present, Velocity will check for a properties file at '/WEB-INF/velocity.properties'. If no file is found there, then Velocity is initialized with the settings in the classpath at 'org.apache.velocity.tools.view.velocity.properties'.
- Version:
- $Id: VelocityView.java 511959 2007-02-26 19:24:39Z nbubna $
- Author:
- Dave Bryson, Jon S. Stevens, Gabe Sidler, Geir Magnusson Jr., Kent Johnson, Daniel Rall, Nathan Bubna
-
-
Field Summary
Fields Modifier and Type Field Description static StringCLEAN_CONFIGURATION_KEYControls removal of tools or data with invalid configurations before initialization is finished.static StringCONTENT_TYPE_KEYThe HTTP content type context key.static StringDEFAULT_CONTENT_TYPEThe default content type for the responsestatic StringDEFAULT_OUTPUT_ENCODINGDefault encoding for the output streamstatic StringDEFAULT_PROPERTIES_PATHDefault Runtime properties.static StringDEPRECATED_TOOLS_KEYDeprecated.static StringDEPRECATED_USER_TOOLS_PATHDeprecated.static StringDEPRECATION_SUPPORT_MODE_KEYControls support for deprecated tools and configuration.static StringLOAD_DEFAULTS_KEYControls loading of available default tool configurations provided by VelocityTools.static StringPROPERTIES_KEYThis is the string that is looked for when getInitParameter is called ("org.apache.velocity.properties").static StringSERVLET_CONTEXT_KEYKey used to access the ServletContext in the Velocity application attributes.static StringTOOLS_KEYKey used to access the toolbox configuration file path from the Servlet or webapp init parameters ("org.apache.velocity.tools") or to access a liveFactoryConfigurationpreviously placed in the ServletContext attributes.static StringUSER_OVERWRITE_KEYControls whether or not templates can overwrite tool and servlet API variables in the local context.static StringUSER_PROPERTIES_PATHDefault velocity properties file path.static StringUSER_TOOLS_PATHDefault toolbox configuration file path.-
Fields inherited from class org.apache.velocity.tools.view.ViewToolManager
CREATE_SESSION_PROPERTY, DEFAULT_TOOLBOX_KEY, PUBLISH_TOOLBOXES_PROPERTY, servletContext
-
Fields inherited from class org.apache.velocity.tools.ToolManager
factory, velocity
-
-
Constructor Summary
Constructors Constructor Description VelocityView(javax.servlet.FilterConfig config)VelocityView(javax.servlet.ServletConfig config)VelocityView(javax.servlet.ServletContext context)VelocityView(JeeConfig config)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected voidconfigure(JeeConfig config, org.apache.velocity.app.VelocityEngine velocity)protected voidconfigure(JeeConfig config, ToolboxFactory factory)Here's the configuration lookup/loading order: If deprecationSupportMode is true: Config file optionally specified byorg.apache.velocity.toolboxinit-param (servlet or servletContext) If none, config file optionally at/WEB-INF/toolbox.xml(deprecated conventional location) If no old toolbox or loadDefaults is true,ConfigurationUtils.getDefaultTools()ConfigurationUtils.getAutoLoaded()(false) Config file optionally specified by servletContextorg.apache.velocity.toolsinit-param Config file optionally at/WEB-INF/tools.xml(new conventional location) Config file optionally specified by servletorg.apache.velocity.toolsinit-param Remember that as these configurations are added on top of each other, the newer values will always override the older ones.ViewToolContextcreateContext(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)Creates and returns an initialized Velocity context.protected FactoryConfigurationgetConfiguration(String path)protected FactoryConfigurationgetConfiguration(String path, boolean required)StringgetDefaultContentType()Returns the configured default Content-Type.protected FactoryConfigurationgetDeprecatedConfig(JeeConfig config)Deprecated.protected InputStreamgetInputStream(String path, boolean required)protected org.apache.commons.collections.ExtendedPropertiesgetProperties(String path)protected org.apache.commons.collections.ExtendedPropertiesgetProperties(String path, boolean required)protected StringgetProperty(String key, String alternate)Simplifies process of getting a property from VelocityEngine, because the VelocityEngine interface sucks compared to the singleton's.org.apache.velocity.TemplategetTemplate(String name)Retrieves the requested template.org.apache.velocity.TemplategetTemplate(String name, String encoding)Retrieves the requested template with the specified character encoding.org.apache.velocity.TemplategetTemplate(javax.servlet.http.HttpServletRequest request)Gets the requested template.org.apache.velocity.TemplategetTemplate(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)protected voidinit(JeeConfig config)Initializes ToolboxFactory, VelocityEngine, and sets default encoding for processing requests.protected voidinit(JeeConfig config, org.apache.velocity.app.VelocityEngine velocity)Initializes the Velocity runtime, first calling loadConfiguration(JeeConfig) to get a org.apache.commons.collections.ExtendedProperties of configuration information and then calling velocityEngine.init().voidmerge(org.apache.velocity.Template template, org.apache.velocity.context.Context context, Writer writer)Merges the template with the context.protected voidperformMerge(org.apache.velocity.Template template, org.apache.velocity.context.Context context, Writer writer)This is here so developers may override it and gain access to the Writer which the template will be merged into.org.apache.velocity.context.Contextrender(javax.servlet.http.HttpServletRequest request, Writer out)org.apache.velocity.context.Contextrender(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)voidsetDefaultContentType(String type)Sets the configured default Content-Type.protected voidsetDeprecationSupportMode(boolean support)Deprecated.protected voidsetEncoding(JeeConfig config)voidsetVelocityEngine(org.apache.velocity.app.VelocityEngine engine)Overrides super class to ensure engine is not set to null.-
Methods inherited from class org.apache.velocity.tools.view.ViewToolManager
addToolboxes, autoConfigure, configure, createContext, findConfig, getCreateSession, getPublishToolboxes, getSessionToolbox, getToolboxKey, hasSessionTools, prepareContext, publishApplicationTools, publishToolboxes, publishToolboxes, setCreateSession, setPublishToolboxes, setToolboxKey, unpublishApplicationTools, updateGlobalProperties
-
Methods inherited from class org.apache.velocity.tools.ToolManager
configure, createContext, createToolbox, debug, getApplicationToolbox, getLog, getRequestToolbox, getToolboxFactory, getUserCanOverwriteTools, getVelocityEngine, hasApplicationTools, hasRequestTools, hasTools, prepareContext, setToolboxFactory, setUserCanOverwriteTools
-
-
-
-
Field Detail
-
CONTENT_TYPE_KEY
public static final String CONTENT_TYPE_KEY
The HTTP content type context key.- See Also:
- Constant Field Values
-
SERVLET_CONTEXT_KEY
public static final String SERVLET_CONTEXT_KEY
Key used to access the ServletContext in the Velocity application attributes.
-
DEFAULT_CONTENT_TYPE
public static final String DEFAULT_CONTENT_TYPE
The default content type for the response- See Also:
- Constant Field Values
-
DEFAULT_OUTPUT_ENCODING
public static final String DEFAULT_OUTPUT_ENCODING
Default encoding for the output stream- See Also:
- Constant Field Values
-
TOOLS_KEY
public static final String TOOLS_KEY
Key used to access the toolbox configuration file path from the Servlet or webapp init parameters ("org.apache.velocity.tools") or to access a liveFactoryConfigurationpreviously placed in the ServletContext attributes.- See Also:
- Constant Field Values
-
DEPRECATED_TOOLS_KEY
@Deprecated public static final String DEPRECATED_TOOLS_KEY
Deprecated.- See Also:
- Constant Field Values
-
USER_TOOLS_PATH
public static final String USER_TOOLS_PATH
Default toolbox configuration file path. If no alternate value for this is specified, the servlet will look here.- See Also:
- Constant Field Values
-
DEPRECATED_USER_TOOLS_PATH
@Deprecated public static final String DEPRECATED_USER_TOOLS_PATH
Deprecated.- See Also:
- Constant Field Values
-
DEFAULT_PROPERTIES_PATH
public static final String DEFAULT_PROPERTIES_PATH
Default Runtime properties.- See Also:
- Constant Field Values
-
PROPERTIES_KEY
public static final String PROPERTIES_KEY
This is the string that is looked for when getInitParameter is called ("org.apache.velocity.properties").- See Also:
- Constant Field Values
-
USER_PROPERTIES_PATH
public static final String USER_PROPERTIES_PATH
Default velocity properties file path. If no alternate value for this is specified, the servlet will look here.- See Also:
- Constant Field Values
-
LOAD_DEFAULTS_KEY
public static final String LOAD_DEFAULTS_KEY
Controls loading of available default tool configurations provided by VelocityTools. The default behavior is conditional; ifDEPRECATION_SUPPORT_MODE_KEYhas not been set tofalseand there is an oldtoolbox.xmlconfiguration present, then the defaults will not be loaded unless you explicitly set this property totruein your init params. If there is notoolbox.xmland/or the deprecation support is turned off, then the default tools will be loaded automatically unless you explicitly set this property tofalsein your init params.- See Also:
- Constant Field Values
-
CLEAN_CONFIGURATION_KEY
public static final String CLEAN_CONFIGURATION_KEY
Controls removal of tools or data with invalid configurations before initialization is finished. The default is false; set totrueto turn this feature on.- See Also:
- Constant Field Values
-
USER_OVERWRITE_KEY
public static final String USER_OVERWRITE_KEY
Controls whether or not templates can overwrite tool and servlet API variables in the local context. The default is true; set tofalseto prevent overwriting of any tool variables.- See Also:
- Constant Field Values
-
DEPRECATION_SUPPORT_MODE_KEY
public static final String DEPRECATION_SUPPORT_MODE_KEY
Controls support for deprecated tools and configuration. The default istrue; set tofalseto turn off support for deprecated tools and configuration.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
VelocityView
public VelocityView(javax.servlet.ServletConfig config)
-
VelocityView
public VelocityView(javax.servlet.FilterConfig config)
-
VelocityView
public VelocityView(javax.servlet.ServletContext context)
-
VelocityView
public VelocityView(JeeConfig config)
-
-
Method Detail
-
setDeprecationSupportMode
@Deprecated protected final void setDeprecationSupportMode(boolean support)
Deprecated.
-
setVelocityEngine
public void setVelocityEngine(org.apache.velocity.app.VelocityEngine engine)
Overrides super class to ensure engine is not set to null.- Overrides:
setVelocityEnginein classToolManager
-
getDefaultContentType
public String getDefaultContentType()
Returns the configured default Content-Type.
-
setDefaultContentType
public void setDefaultContentType(String type)
Sets the configured default Content-Type.
-
getProperty
protected String getProperty(String key, String alternate)
Simplifies process of getting a property from VelocityEngine, because the VelocityEngine interface sucks compared to the singleton's. Use of this method assumes thatinit(JeeConfig,VelocityEngine)has already been called.
-
init
protected void init(JeeConfig config)
Initializes ToolboxFactory, VelocityEngine, and sets default encoding for processing requests.
NOTE: If no charset is specified in the default.contentType property (in your velocity.properties) and you have specified an output.encoding property, then that will be used as the charset for the default content-type of pages served by this servlet.
- Parameters:
config- servlet configuation
-
init
protected void init(JeeConfig config, org.apache.velocity.app.VelocityEngine velocity)
Initializes the Velocity runtime, first calling loadConfiguration(JeeConfig) to get a org.apache.commons.collections.ExtendedProperties of configuration information and then calling velocityEngine.init(). Override this to do anything to the environment before the initialization of the singleton takes place, or to initialize the singleton in other ways.- Parameters:
config- servlet configuration parameters
-
configure
protected void configure(JeeConfig config, org.apache.velocity.app.VelocityEngine velocity)
-
configure
protected void configure(JeeConfig config, ToolboxFactory factory)
Here's the configuration lookup/loading order:- If deprecationSupportMode is true:
- Config file optionally specified by
org.apache.velocity.toolboxinit-param (servlet or servletContext) - If none, config file optionally at
/WEB-INF/toolbox.xml(deprecated conventional location)
- Config file optionally specified by
- If no old toolbox or loadDefaults is true,
ConfigurationUtils.getDefaultTools() ConfigurationUtils.getAutoLoaded()(false)- Config file optionally specified by servletContext
org.apache.velocity.toolsinit-param - Config file optionally at
/WEB-INF/tools.xml(new conventional location) - Config file optionally specified by servlet
org.apache.velocity.toolsinit-param
org.apache.velocity.tools.cleanConfigurationinit-param to true in either your servlet or servletContext init-params. - If deprecationSupportMode is true:
-
getDeprecatedConfig
@Deprecated protected FactoryConfiguration getDeprecatedConfig(JeeConfig config)
Deprecated.First tries to find a path to a toolbox under the deprecatedorg.apache.velocity.toolboxkey. If found, it tries to load the configuration there and will blow up if there is no config file there. If not found, it looks for a config file at /WEB-INF/toolbox.xml (the deprecated default location) and tries to load it if found.
-
getInputStream
protected InputStream getInputStream(String path, boolean required)
-
getProperties
protected org.apache.commons.collections.ExtendedProperties getProperties(String path)
-
getProperties
protected org.apache.commons.collections.ExtendedProperties getProperties(String path, boolean required)
-
getConfiguration
protected FactoryConfiguration getConfiguration(String path)
-
getConfiguration
protected FactoryConfiguration getConfiguration(String path, boolean required)
-
setEncoding
protected void setEncoding(JeeConfig config)
-
render
public org.apache.velocity.context.Context render(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws IOException- Parameters:
request- HttpServletRequest object containing client requestresponse- HttpServletResponse object for the response- Returns:
- the
Contextprepared and used to perform the rendering to allow proper cleanup afterward - Throws:
IOException
-
render
public org.apache.velocity.context.Context render(javax.servlet.http.HttpServletRequest request, Writer out) throws IOException- Throws:
IOException
-
createContext
public ViewToolContext createContext(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Creates and returns an initialized Velocity context.
A new context of classViewToolContextis created and initialized.- Overrides:
createContextin classViewToolManager- Parameters:
request- servlet request from clientresponse- servlet reponse to client
-
getTemplate
public org.apache.velocity.Template getTemplate(javax.servlet.http.HttpServletRequest request)
Gets the requested template.
- Parameters:
request- client request- Returns:
- Velocity Template object or null
-
getTemplate
public org.apache.velocity.Template getTemplate(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
-
getTemplate
public org.apache.velocity.Template getTemplate(String name)
Retrieves the requested template.- Parameters:
name- The file name of the template to retrieve relative to the template root.- Returns:
- The requested template.
- Throws:
org.apache.velocity.exception.ResourceNotFoundException- if template not found from any available source.ParseErrorException- if template cannot be parsed due to syntax (or other) error.
-
getTemplate
public org.apache.velocity.Template getTemplate(String name, String encoding)
Retrieves the requested template with the specified character encoding.- Parameters:
name- The file name of the template to retrieve relative to the template root.encoding- the character encoding of the template- Returns:
- The requested template.
- Throws:
org.apache.velocity.exception.ResourceNotFoundException- if template not found from any available source.ParseErrorException- if template cannot be parsed due to syntax (or other) error.
-
merge
public void merge(org.apache.velocity.Template template, org.apache.velocity.context.Context context, Writer writer) throws IOExceptionMerges the template with the context. Only override this if you really, really really need to. (And don't call us with questions if it breaks :)- Parameters:
template- template being renderedcontext- Context created by thecreateContext(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)writer- into which the content is rendered- Throws:
IOException
-
performMerge
protected void performMerge(org.apache.velocity.Template template, org.apache.velocity.context.Context context, Writer writer) throws IOExceptionThis is here so developers may override it and gain access to the Writer which the template will be merged into. See VELTOOLS-7 for discussion of this.- Parameters:
template- template object returned by the handleRequest() methodcontext- Context created by thecreateContext(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)writer- a VelocityWriter that the template is merged into- Throws:
IOException
-
-