,ch14.9433  Page 471  Monday, February 14, 2005  12:55 PM
Solution
Integrate the Velocity template engine into your Struts application, replacing your
JSP pages with Velocity templates.
Discussion
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
response.
Here's an action, adapted from the Struts MailReader example, that forwards to a
Velocity template to display the  Welcome  page:
           type="org.apache.struts.webapp.example.WelcomeAction">
    
    
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 
INF/lib directory.
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
    velocity
    
        org.apache.velocity.tools.view.servlet.VelocityViewServlet
    
Integrating Struts and Velocity | 471
This is the Title of the Book, eMatter Edition
Copyright   2005 O'Reilly & Associates, Inc. All rights reserved.






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

strutsweb hosting

 

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved