,ch14.9433 Page 471 Monday, February 14, 2005 12:55 PM
Integrate the Velocity template engine into your Struts application, replacing your
JSP pages with Velocity templates.
The Velocity template engine (http://jakarta.apache.org/velocity) can create any kind
of textual document by merging Java objects with a template. The template docu
ment contains special markup that Velocity parses and replaces with values from
Java objects stored in a template context.
When you use Velocity with Struts, instead of forwarding to JSP pages, you forward
to a Velocity template. The Velocity servlet services the request, processing the tem
plate through the Velocity template engine and returning the generated HTML as the
Here's an action, adapted from the Struts MailReader example, that forwards to a
Velocity template to display the Welcome page:
To use Velocity with Struts, you'll need to download the Velocity distribution from
http://jakarta.apache.org/velocity. You also need to download the VelocityTools from
http://jakarta.apache.org/velocity/tools/. VelocityTools includes helper classes that
allow you to reference Struts managed objects such as the action form and message
resources. From the Velocity distribution, copy the velocity 1.4.jar and the velocity
dep 1.4.jar from the Velocity distribution directory to your application's WEB INF/
lib directory. For the VelocityTools /lib directory, copy the velocity tools 1.1.jar,
velocity tools generic 1.1.jar, velocity tools view 1.1.jar to your application's WEB
Next, declare the Velocity view servlet and servlet mapping in your web.xml file. Tra
ditionally, the .vm extension is used for Velocity templates. Example 14 26 shows
the portion of a web.xml file that configure the Velocity servlet.
Example 14 26. VelocityViewServlet declaration and mapping
Integrating Struts and Velocity | 471
This is the Title of the Book, eMatter Edition
Copyright 2005 O'Reilly & Associates, Inc. All rights reserved.