org.apache.velocity.tools.view.tools
Class ParameterParser

java.lang.Object
  extended by org.apache.velocity.tools.generic.ValueParser
      extended by org.apache.velocity.tools.view.tools.ParameterParser

public class ParameterParser
extends ValueParser

Utility class for easy parsing of ServletRequest parameters.

 Template example(s):
   $params.foo                ->  bar
   $params.getNumber('baz')   ->  12.6
   $params.getInt('baz')      ->  12
   $params.getNumbers('baz')  ->  [12.6]

 Toolbox configuration:
 <tool>
   <key>params</key>
   <scope>request</scope>
   <class>org.apache.velocity.tools.view.tools.ParameterParser</class>
 </tool>
 

When used as a view tool, this should only be used in the request scope. This class is, however, quite useful in your application's controller, filter, or action code as well as in templates.

Version:
$Revision: 483666 $ $Date: 2006-12-07 13:04:59 -0800 (Thu, 07 Dec 2006) $
Author:
Nathan Bubna

Field Summary
private  javax.servlet.ServletRequest request
           
 
Constructor Summary
ParameterParser()
          Constructs a new instance
ParameterParser(javax.servlet.ServletRequest request)
          Constructs a new instance using the specified request.
 
Method Summary
 java.util.Map getAll()
          Returns the map of all parameters available for the current request.
protected  javax.servlet.ServletRequest getRequest()
          Returns the current ServletRequest for this instance.
protected  java.util.Map getSource()
          Overrides ValueParser.getSource() to return the result of getRequest().getParameterMap().
 java.lang.String getString(java.lang.String key)
          Overrides ValueParser.getString(String key) to retrieve the String from the ServletRequest instead of an arbitrary Map.
 java.lang.String[] getStrings(java.lang.String key)
          Overrides ValueParser.getString(String key) to retrieve Strings from the ServletRequest instead of an arbitrary Map.
 void init(java.lang.Object obj)
          Initializes this instance.
protected  void setRequest(javax.servlet.ServletRequest request)
          Sets the current ServletRequest
protected  void setSource(java.util.Map source)
          Overrides ValueParser.setSource(Map source) to throw an UnsupportedOperationException, because this class uses a servlet request as its source, not a Map.
 
Methods inherited from class org.apache.velocity.tools.generic.ValueParser
exists, get, getBoolean, getBoolean, getBoolean, getBooleans, getDouble, getDouble, getDouble, getDoubles, getInt, getInteger, getInteger, getInts, getLocale, getLocale, getLocales, getNumber, getNumber, getNumbers, getString, getStringsDelimiter, parseBoolean, parseLocale, parseNumber, parseStringList, setStringsDelimiter
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

request

private javax.servlet.ServletRequest request
Constructor Detail

ParameterParser

public ParameterParser()
Constructs a new instance


ParameterParser

public ParameterParser(javax.servlet.ServletRequest request)
Constructs a new instance using the specified request.

Parameters:
request - the ServletRequest to be parsed
Method Detail

init

public void init(java.lang.Object obj)
Initializes this instance.

Parameters:
obj - the current ViewContext or ServletRequest
Throws:
java.lang.IllegalArgumentException - if the param is not a ViewContext or ServletRequest

setRequest

protected void setRequest(javax.servlet.ServletRequest request)
Sets the current ServletRequest

Parameters:
request - the ServletRequest to be parsed

getRequest

protected javax.servlet.ServletRequest getRequest()
Returns the current ServletRequest for this instance.

Returns:
the current ServletRequest
Throws:
java.lang.UnsupportedOperationException - if the request is null

getString

public java.lang.String getString(java.lang.String key)
Overrides ValueParser.getString(String key) to retrieve the String from the ServletRequest instead of an arbitrary Map.

Overrides:
getString in class ValueParser
Parameters:
key - the parameter's key
Returns:
parameter matching the specified key or null if there is no matching parameter

getStrings

public java.lang.String[] getStrings(java.lang.String key)
Overrides ValueParser.getString(String key) to retrieve Strings from the ServletRequest instead of an arbitrary Map.

Overrides:
getStrings in class ValueParser
Parameters:
key - the key for the desired parameter
Returns:
an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist

setSource

protected void setSource(java.util.Map source)
Overrides ValueParser.setSource(Map source) to throw an UnsupportedOperationException, because this class uses a servlet request as its source, not a Map.

Overrides:
setSource in class ValueParser

getSource

protected java.util.Map getSource()
Overrides ValueParser.getSource() to return the result of getRequest().getParameterMap().

Overrides:
getSource in class ValueParser

getAll

public java.util.Map getAll()
Returns the map of all parameters available for the current request.



Copyright (c) 2003-2007 Apache Software Foundation