|
Helping ordinary people create extraordinary websites! |
Client and server-side templating with VelocityBy Sing Li2004-06-13
Interoperating with the Struts framework Struts is a popular Web application building framework based on the MVC model. The default view component technology for Struts is JSP technology. However, you can easily integrate Velocity as the view component. Figure 1 illustrates this specific use of Velocity: Figure 1. Integrating Velocity with the Struts MVC framework
It is important to note that Velocity does not displace JSP technology in this composition. Instead, JSP technology and Velocity templates can work alongside one another. To integrate Velocity, configure VelocityViewServlet to process .vm templates, as we described in the Deploying VelocityViewServlet section. This means that .jsp files will continue to be processed by the container (that is, Jasper in Tomcat 5), while any .vm templates are passed to Velocity. The VelocityStruts component of the Velocity Tools subproject (see Resources) has everything you need to integrate Velocity with Struts. VelocityStruts provides a set of specialized Velocity tools to access Struts-specific resources and information within a Velocity template. Table 3 provides a brief list of the most frequently used tools: Table 3. Tools for VelocityStruts integration
There is also a set of tools available specifically for working with the new features in Struts 1.1, as shown in Table 4: Table 4. Specialized Struts 1.1 access tools
In the webapps\struts-example directory, you will see an example of using Struts instead of JSP technology to create Struts pages. In this case, we replace the first title page of the example Web application distributed with Struts. You may want to try your hand at replacing other pages. The following list describes the steps involved.
4. Last but not least, copy the toolbox.xml and velocity.properties files from this article's source code download (see Resources) to the WEB-INF directory. The new index.vm file is shown in Listing 24. You may want to compare this with the original index.jsp file. Listing 24. Struts interoperation through use of the index.vm Velocity template
In index.vm, the message tool in $msg is used throughout this template to access Struts's locale-dependent, internationalized resources. This approach eliminates most hard-coded strings in the template, localizing changes to the resource bundle containing the internationalized strings. You can use the conditional #if directive of VTL to directly check for the existence of the database attribute in the servlet context. The $application reference can be used to access any attribute in the servlet context ($request, $response, and $session are also available to access attributes of other Servlet API objects). The setURI() method of the LinkTool is used to generate a server-side URI link to Struts's actions, as well as to the "Powered by Velocity" logo image. Note the use of the addQueryData() method of the LinkTool to append additional action information to a resulting URI. You can test the Velocity page by starting Tomcat 5 and accessing the http://localhost:8080/struts-example/ URL. Note how it works identically to the original JSP version. Tutorial Pages: » Client and server-side templating with Velocity » Basic template engine operation » Velocity contexts » Velocity as a standalone parser » Velocity vs. JSP technology on the server » Deploying Velocity with Tomcat 5 » Interoperating with the Struts framework » Conclusions » Resources First published by IBM developerWorks |
|