Class ViewToolContext

  • All Implemented Interfaces:
    org.apache.velocity.context.Context, ViewContext

    public class ViewToolContext
    extends org.apache.velocity.tools.ToolContext
    implements ViewContext

    ToolContext implementation 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:

    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
    • 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 void addToolboxesUnderKey​(java.lang.String toolboxKey)  
      boolean containsKey​(java.lang.String key)
      Indicates whether the specified key is in the context.
      java.lang.Object get​(java.lang.String key)
      Looks up and returns the object with the specified key.
      java.lang.Object getAttribute​(java.lang.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.HttpServletRequest getRequest()  
      javax.servlet.http.HttpServletResponse getResponse()  
      protected java.lang.Object getServletApi​(java.lang.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.ServletContext getServletContext()  
      javax.servlet.http.HttpSession getSession()  
      protected java.util.List<org.apache.velocity.tools.Toolbox> getToolboxes()  
      protected java.lang.Object getToolVar​(java.lang.String key)
      Finds the automatically provided values, either configured tools or servlet API objects (request, response, etc).
      protected java.lang.Object getUserVar​(java.lang.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()  
      org.apache.velocity.app.VelocityEngine getVelocityEngine()  
      protected void putToolProperties()  
      protected void setToolboxKey​(java.lang.String key)  
      • Methods inherited from class org.apache.velocity.tools.ToolContext

        addToolbox, 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,
                               javax.servlet.http.HttpServletRequest request,
                               javax.servlet.http.HttpServletResponse response,
                               javax.servlet.ServletContext application)
    • Method Detail

      • setToolboxKey

        protected void setToolboxKey​(java.lang.String key)
      • putToolProperties

        protected void putToolProperties()
      • getToolboxes

        protected java.util.List<org.apache.velocity.tools.Toolbox> getToolboxes()
        Overrides:
        getToolboxes in class org.apache.velocity.tools.ToolContext
      • addToolboxesUnderKey

        protected void addToolboxesUnderKey​(java.lang.String toolboxKey)
      • get

        public java.lang.Object get​(java.lang.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 org.apache.velocity.tools.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 java.lang.Object getUserVar​(java.lang.String key)
        Finds "user" set values, either in the local context or in the scoped attributes if none is in the local context.
        Parameters:
        key - attribute name
        Returns:
        found value or null
        See Also:
        ToolContext.internalGet(java.lang.String), getAttribute(java.lang.String)
      • getToolVar

        protected java.lang.Object getToolVar​(java.lang.String key)
        Finds the automatically provided values, either configured tools or servlet API objects (request, response, etc).
        Parameters:
        key - attribute key
        Returns:
        found configured value or null
        See Also:
        ToolContext.findTool(java.lang.String), getServletApi(java.lang.String)
      • getServletApi

        protected java.lang.Object getServletApi​(java.lang.String key)
        Returns the current matching servlet request, response, session, or servlet context instance, or null if the key matches none of those keys.
        Parameters:
        key - attribute key
        Returns:
        found value or null
      • getAttribute

        public java.lang.Object getAttribute​(java.lang.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
        Parameters:
        key - attribute key
        Returns:
        found value or null
        Since:
        VelocityTools 1.1
      • getRequest

        public javax.servlet.http.HttpServletRequest getRequest()
        Specified by:
        getRequest in interface ViewContext
        Returns:
        the current servlet request.
      • getResponse

        public javax.servlet.http.HttpServletResponse getResponse()
        Specified by:
        getResponse in interface ViewContext
        Returns:
        the current servlet response.
      • getSession

        public javax.servlet.http.HttpSession getSession()
        Returns:
        the current session, if any.
      • getServletContext

        public javax.servlet.ServletContext getServletContext()
        Specified by:
        getServletContext in interface ViewContext
        Returns:
        the servlet context.
      • getVelocityContext

        public org.apache.velocity.context.Context getVelocityContext()
        Specified by:
        getVelocityContext in interface ViewContext
        Returns:
        a reference to the Velocity context (this object).
      • getVelocityEngine

        public org.apache.velocity.app.VelocityEngine getVelocityEngine()
        Specified by:
        getVelocityEngine in interface ViewContext
        Returns:
        a reference to the VelocityEngine.
      • containsKey

        public boolean containsKey​(java.lang.String key)
        Indicates whether the specified key is in the context.
        Specified by:
        containsKey in interface org.apache.velocity.context.Context
        Overrides:
        containsKey in class org.apache.velocity.tools.ToolContext
        Parameters:
        key - The key to look for.
        Returns:
        Whether the key is in the context.