Class ViewToolContext
- java.lang.Object
-
- org.apache.velocity.tools.ToolContext
-
- org.apache.velocity.tools.view.ViewToolContext
-
- All Implemented Interfaces:
org.apache.velocity.context.Context,ViewContext
- Direct Known Subclasses:
ChainedContext,JspToolContext
public class ViewToolContext extends ToolContext implements ViewContext
ToolContextimplementation specific to the servlet environment.It provides the following special features:
- puts the request, response, session, and servlet context objects into the Velocity context for direct access, and keeps them read-only
- supports a read-only toolbox of view tools
- auto-searches servlet request attributes, session attributes and servlet context attribues for objects
The
get(String key)method implements the following search order for objects:- tool in a request scoped toolbox
- tool in a session scoped toolbox
- tool in a application scoped toolbox
- request, response, session, or servlet context
- object in the local map of objects (traditional use)
- request attributes, session attributes, servlet context attributes
The purpose of this class is to make it easy for web designer to work with Java servlet based web applications. They do not need to be concerned with the concepts of request, session or application attributes and the lifetime of objects in these scopes.
Note that the put() method always puts objects into the local map and does not allow tools or servlet classes to be overridden.
- Version:
- $Id: ViewContext.java 514727 2007-03-05 16:49:03Z nbubna $
- Author:
- Geir Magnusson Jr., Gabe Sidler, Nathan Bubna
-
-
Field Summary
-
Fields inherited from class org.apache.velocity.tools.ToolContext
CATCH_EXCEPTIONS_KEY, CONTEXT_KEY, ENGINE_KEY, LOCALE_KEY, LOG_KEY, PATH_KEY
-
Fields inherited from interface org.apache.velocity.tools.view.ViewContext
APPLICATION, DEFAULT_TOOLBOX_KEY, REQUEST, RESPONSE, SERVLET_CONTEXT_KEY, SESSION
-
-
Constructor Summary
Constructors Constructor Description ViewToolContext(org.apache.velocity.app.VelocityEngine velocity, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.ServletContext application)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddToolboxesUnderKey(String toolboxKey)booleancontainsKey(String key)Indicates whether the specified key is in the context.Objectget(String key)Looks up and returns the object with the specified key.ObjectgetAttribute(String key)Searches for the named attribute in request, session (if valid), and application scope(s) in order and returns the value associated or null.javax.servlet.http.HttpServletRequestgetRequest()Returns the current servlet request.javax.servlet.http.HttpServletResponsegetResponse()Returns the current servlet response.protected ObjectgetServletApi(String key)Returns the current matching servlet request, response, session, or servlet context instance, or null if the key matches none of those keys.javax.servlet.ServletContextgetServletContext()Returns the servlet context.javax.servlet.http.HttpSessiongetSession()Returns the current session, if any.protected List<Toolbox>getToolboxes()protected ObjectgetToolVar(String key)Finds the automatically provided values, either configured tools or servlet API objects (request, response, etc).protected ObjectgetUserVar(String key)Finds "user" set values, either in the local context or in the scoped attributes if none is in the local context.org.apache.velocity.context.ContextgetVelocityContext()Returns a reference to the Velocity context (this object).org.apache.velocity.app.VelocityEnginegetVelocityEngine()Returns a reference to the VelocityEngine.protected voidputToolProperties()protected voidsetToolboxKey(String key)-
Methods inherited from class org.apache.velocity.tools.ToolContext
addToolbox, containsKey, findTool, getKeys, getToolbox, getToolClassMap, getToolProperties, getUserCanOverwriteTools, internalGet, keySet, put, putAll, putToolProperties, putToolProperty, putVelocityEngine, remove, setUserCanOverwriteTools
-
-
-
-
Method Detail
-
setToolboxKey
protected void setToolboxKey(String key)
-
putToolProperties
protected void putToolProperties()
-
getToolboxes
protected List<Toolbox> getToolboxes()
- Overrides:
getToolboxesin classToolContext
-
addToolboxesUnderKey
protected void addToolboxesUnderKey(String toolboxKey)
-
get
public Object get(String key)
Looks up and returns the object with the specified key.
See the class documentation for more details.
- Specified by:
getin interfaceorg.apache.velocity.context.Context- Overrides:
getin classToolContext- Parameters:
key- the key of the object requested- Returns:
- the requested object or null if not found
- See Also:
ToolContext.setUserCanOverwriteTools(boolean),getUserVar(java.lang.String),getToolVar(java.lang.String)
-
getUserVar
protected Object getUserVar(String key)
Finds "user" set values, either in the local context or in the scoped attributes if none is in the local context.
-
getToolVar
protected Object getToolVar(String key)
Finds the automatically provided values, either configured tools or servlet API objects (request, response, etc).
-
getServletApi
protected Object getServletApi(String key)
Returns the current matching servlet request, response, session, or servlet context instance, or null if the key matches none of those keys.
-
getAttribute
public Object getAttribute(String key)
Searches for the named attribute in request, session (if valid), and application scope(s) in order and returns the value associated or null.
- Specified by:
getAttributein interfaceViewContext- Since:
- VelocityTools 1.1
-
getRequest
public javax.servlet.http.HttpServletRequest getRequest()
Returns the current servlet request.
- Specified by:
getRequestin interfaceViewContext
-
getResponse
public javax.servlet.http.HttpServletResponse getResponse()
Returns the current servlet response.
- Specified by:
getResponsein interfaceViewContext
-
getSession
public javax.servlet.http.HttpSession getSession()
Returns the current session, if any.
-
getServletContext
public javax.servlet.ServletContext getServletContext()
Returns the servlet context.
- Specified by:
getServletContextin interfaceViewContext
-
getVelocityContext
public org.apache.velocity.context.Context getVelocityContext()
Returns a reference to the Velocity context (this object).
- Specified by:
getVelocityContextin interfaceViewContext
-
getVelocityEngine
public org.apache.velocity.app.VelocityEngine getVelocityEngine()
Returns a reference to the VelocityEngine.
- Specified by:
getVelocityEnginein interfaceViewContext
-
containsKey
public boolean containsKey(String key)
Indicates whether the specified key is in the context.- Parameters:
key- The key to look for.- Returns:
- Whether the key is in the context.
-
-