The toolbox file, WEB-INF/toolbox.xml by
default, maps names of our choosing to the classes that they will
represent. It's easier than that sounds.
Think about asking our friend Jon to grab us a 'wrench' from a
real toolbox. Jon just needs to know which wrench we want (metric,
pipe, crescent etc,). He doesn't need to know what the wrench does
nor what we are planning to do with it.
The Velocity Toolbox works the same way, we must only specify
which tool to assign to a name, and then the Velocity engine takes
care of the rest by making any public method available to the
template. For example, from the definitions below, the template
could call $wrench.getSize() or $wrench.size .
PipeWrench.java
public class PipeWrench {
public String getSize() {
return "Large Pipe Wrench!";
}
}
|
|
toolbox.xml
<?xml version="1.0"?>
<toolbox>
<tool>
<key>wrench</key>
<scope>application</scope>
<class>PipeWrench</class>
</tool>
</toolbox>
|
|
Tool Scopes
The toolbox support built into the VelocityViewServlet also provides
support for specifying the scope of your tool with regards to the
servlet environment. Tools may be placed within the request,
session, or application scopes of your web app.
The scope that you set for your tool will determine both its
lifecycle and, if the tool has a public init(Object) method,
then the scope will also determine what data is passed to that method:
- application scoped tools will be instantiated only once and then
reused for each request. Due to this, it is strongly encouraged
that your application scoped tools be completely threadsafe. The MathTool
in the GenericTools section is a good example of tool meant to be application
scoped. If an application scoped tool implements ViewTool, then the
javax.servlet.ServletContext for the webapp will be passed to its
init(Object) method after it is instantiated.
- session scoped tools are instantiated once per unique session and
are then reused for every request associated with that particular session. If
a session scoped tool implements ViewTool, then its
init(Object)
method will be passed the
ViewContext
of the request during which the session was created.
- request is the default scope. If no scope is specified for a
<tool> in your toolbox.xml, then it will be automatically set as
request scope. Tools with this scope are instantiated for every
servlet request fed to the VelocityViewServlet. If a request scoped tool
implements ViewTool, then its
init(Object) method will be
passed the current ViewContext.
You can specify the scope of your tools by adding a <scope>
element to your toolbox.xml entries like this:
<tool>
<key>math</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.MathTool</class>
</tool>
|
|
Tool Request Paths
You can specify a restriction on the request URIs for which the tool
will be put in the context using the <request-path> element.
It can be an exact request path (matching one page) or end with the
* wildard, meaning that incoming request paths need only
start with the provided value for the tool to be available in the context.
For instance:
<tool>
<key>catalog</key>
<scope>request</scope>
<request-path>/catalog/*</request-path>
<class>com.mycompany.tools.CatalogTool</class>
</tool>
|
|
For now, this request-path filter is only valid for request scoped tools.
In VelocityTools 2.0, we plan to support this for session and application
scoped tools as well.
Tool Parameters
The toolbox support built into the VelocityViewServlet also provides
support for passing configuration parameters to tools that have a
public void configure(java.util.Map params) method.
This method will only be called if parameters have been specified for the tool.
You can specify parameters for your tools by adding a <parameter>
element to your toolbox.xml entries like this:
<tool>
<key>math</key>
<scope>application</scope>
<class>com.foo.tools.MyTool</class>
<parameter name="my.parameter.name" value="my.parameter.value"/>
</tool>
|
|
Toolbox Data
In addition to specifiying arbitrary Java classes as tools
to be automatically available to your templates, the toolbox support
also includes the ability to specify arbitrary strings, booleans, and
numbers to be automatically available in your templates. The format
is as follows:
<?xml version="1.0"?>
<toolbox>
<data type="number">
<key>app_version</key>
<value>0.9</value>
</data>
<data type="string">
<key>app_name</key>
<value>Jon's Tool Shop</value>
</data>
<data type="boolean">
<key>debug</key>
<value>true</value>
</data>
</toolbox>
|
|
As with your tools, your data will be exposed to your templates
under the specified key (e.g. $app_version, $app_name, $debug).
|