org.apache.velocity.tools.view
Class ViewToolContext

java.lang.Object
  extended by org.apache.velocity.tools.ToolContext
      extended by 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

ToolContext implementation specific to the servlet environment.

It provides the following special features:

The get(String key) method implements the following search order for objects:

  1. tool in a request scoped toolbox
  2. tool in a session scoped toolbox
  3. tool in a application scoped toolbox
  4. request, response, session, or servlet context
  5. object in the local map of objects (traditional use)
  6. 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
ViewToolContext(org.apache.velocity.app.VelocityEngine velocity, HttpServletRequest request, HttpServletResponse response, ServletContext application)
           
 
Method Summary
protected  void addToolboxesUnderKey(String toolboxKey)
           
 boolean containsKey(String key)
          Indicates whether the specified key is in the context.
 Object get(String key)
          Looks up and returns the object with the specified key.
 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.
 HttpServletRequest getRequest()
          Returns the current servlet request.
 HttpServletResponse getResponse()
          Returns the current servlet response.
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.
 ServletContext getServletContext()
          Returns the servlet context.
 HttpSession getSession()
          Returns the current session, if any.
protected  List<Toolbox> getToolboxes()
           
protected  Object getToolVar(String key)
          Finds the automatically provided values, either configured tools or servlet API objects (request, response, etc).
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.
 org.apache.velocity.context.Context getVelocityContext()
          Returns a reference to the Velocity context (this object).
 org.apache.velocity.app.VelocityEngine getVelocityEngine()
          Returns a reference to the VelocityEngine.
protected  void putToolProperties()
           
protected  void setToolboxKey(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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ViewToolContext

public ViewToolContext(org.apache.velocity.app.VelocityEngine velocity,
                       HttpServletRequest request,
                       HttpServletResponse response,
                       ServletContext application)
Method Detail

setToolboxKey

protected void setToolboxKey(String key)

putToolProperties

protected void putToolProperties()

getToolboxes

protected List<Toolbox> getToolboxes()
Overrides:
getToolboxes in class ToolContext

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:
get in interface org.apache.velocity.context.Context
Overrides:
get in class ToolContext
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.

See Also:
ToolContext.internalGet(java.lang.String), getAttribute(java.lang.String)

getToolVar

protected Object getToolVar(String key)
Finds the automatically provided values, either configured tools or servlet API objects (request, response, etc).

See Also:
ToolContext.findTool(java.lang.String), getServletApi(java.lang.String)

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:
getAttribute in interface ViewContext
Since:
VelocityTools 1.1

getRequest

public HttpServletRequest getRequest()

Returns the current servlet request.

Specified by:
getRequest in interface ViewContext

getResponse

public HttpServletResponse getResponse()

Returns the current servlet response.

Specified by:
getResponse in interface ViewContext

getSession

public HttpSession getSession()

Returns the current session, if any.


getServletContext

public ServletContext getServletContext()

Returns the servlet context.

Specified by:
getServletContext in interface ViewContext

getVelocityContext

public org.apache.velocity.context.Context getVelocityContext()

Returns a reference to the Velocity context (this object).

Specified by:
getVelocityContext in interface ViewContext

getVelocityEngine

public org.apache.velocity.app.VelocityEngine getVelocityEngine()

Returns a reference to the VelocityEngine.

Specified by:
getVelocityEngine in interface ViewContext

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.


Copyright © 2002-2011 The Apache Software Foundation. All Rights Reserved.