Overview¶
The VelocityStruts sub-project integrates Velocity with the Apache Struts web application framework and enables the use of Velocity templates interchangeably with JSP pages for the view layer.
Various other web application frameworks offer built-in support for Velocity templates. This project provides the minimal glue necessary to give Struts developers an alternative to JSP.
Diagram 1. Comparison of JSP and Velocity for the view layer in a Struts application.
As we follow the typical Struts process flow in Diagram 1, notice how there is relatively little change except for the addition of Velocity. The velocity-struts.jar leverages a standalone Velocity servlet to process template files (specifically, the jar file is velocity-tools-x.x.jar) and uses some drop-in tools to provide transparent access to Struts specific objects (ex. message resources, form beans, errors, links). The action mapping file will simply contain ActionForwards that send control to a Velocity-based View layer instead of sending to a JSP.
Also notable is that Velocity and JSP are not mutually exclusive. Both technologies can be used in the same application without any problems. This allows developers the option of trying Velocity without heavy modification to existing applications.
We're convinced that once you give Velocity a try, you'll really like it.
Default Configuration¶
VelocityStruts includes both the GenericTools and VelocityView and adds tools for use in Struts 1.x applications. These tools match the functions of the key Struts taglibs and provide access to Struts resources, messages, tiles, validation functions and more.
The default configuration provided for VelocityStruts is here, here and here.
Dependencies¶
The dependencies required for VelocityStruts can be found on our dependencies chart.
Tools¶
These are in addition to tools provided by GenericTools and VelocityView.
- ActionMessagesTool - For accessing/displaying Struts action messages.
- ErrorTool - For accessing/displaying Struts error messages.
- FormTool - For working with HTML forms in Struts apps.
- MessageResourcesTool - Abstract view tool that provides access to Struts message resources.
- MessageTool - Provides methods to render Struts application resources (for i18n and other textual content access).
- SecureLinkTool - For using Struts SSL Extensions. It has the same interface as StrutsLinkTool and can function as a substitute if Struts 1.x and SSL Ext are installed.
- StrutsLinkTool - Subclass of LinkTool for creating URI links in Struts by adding support for Struts actions and forwards.
- TilesTool - For using Struts-Tiles (not for Tiles 2!) in Velocity.
- ValidatorTool - Uses Struts-Validator to produce client side javascript validation for your forms.
Examples¶
A Struts application example has been included to demonstrate the use of the VelocityViewServlet with the automatically loaded VelocityStruts tools.
To run the examples you need Tomcat 4+ or a compatible servlet runner.
The 'ant examples' target of the build process automatically generates a ready-to-deploy struts.war archive file located in the examples subdirectory of the distribution. Deploy (i.e. copy) one or both of these war files to the webapps directory of your servlet runner and restart. Now point a web browser at the following url:
http://<your_server>:<port>/struts/
VelocityStruts is also distributed with a couple other examples that use VelocityTools without the Struts 1 tools.
Benefits¶
There are many different reasons why people are choosing Velocity technology for the view layer. Here are some:
- Velocity helps enforce a clean separation between the view layer and the model/control layers. This leads to clean application design and a clear separation of concerns between View Designers and back-end Developers.
- Velocity Template Language (VTL) has few directives and is simple and easy to learn. Most people report being productive within a day.
- Velocity is easy to extend with Tools which are simply any class which has public methods. These are typically much cleaner and easier to develop than JSP custom tag libraries.
- Using the TilesTool it is easy to mix Velocity and JSP tiles in the same page/layout. For those using Tiles, this makes gradual migration between or integration of the two view technologies trivially easy!
- Velocity 'macros' are a powerful tool for the View Designer. They enable the creation of reusable snippets of markup, often eliminating what would have been a need to develop a custom tag library or other server-side tool. + Velocity templates are not limited to HTML and can be used to produce any type of text output including XML, SQL, ASCII, PostScript, etc.
- Velocity enables easy access to dynamic data that Web Designers can understand.
- Velocity being interpreted, enables a simple development cycle where template errors can quickly be localized and debugged.
- Velocity, which caches templates for speed, has been reported to have performance comparable or better than JSP.
- Velocity is supported by an active and helpful community of users and developers.
Success Stories¶
Comments from folks who are using Struts and Velocity:
"We've been using Struts/Velocity for some time now and we find it to be an extremely well matched combination. One of the main advantages of Velocity is that the syntax is incredibly easy. I can explain it to a designer who has never seen it before and expect them to be useful within an hour."
"One of the nicest things it enforces is the separation of presentation layer and business logic. It removes the desire to 'fix' problems by just putting a small change into the JSP via scriptlets. It also makes the code a lot more readable which also reduces the development time."
"Struts is a solid framework and Velocity makes it even better."
"I find that the VelocityTools for Struts works as well, or better, with Struts than another other presentation technology, bar none. In fact, most of the tools work just as well with any other framework you might care to name." Ted Husted (author of Struts in Action)
Licence¶
This software is licensed under the Apache Software License 2.0.