Configuring caching for Struts and Tiles applications

Use this task to cache Struts and Tiles applications.

Before you begin

Before you configure Struts and Tiles caching, you should have a developed application. Find more information about developing Struts and Tiles applications on the Apache Struts Web Application Framework website.

About this task

Use this task when you want to cache data in Struts and Tiles applications.

Struts is an open source framework for building web applications using the Model-View-Controller (MVC) architecture. The Struts framework has a controller component and integrates with other technologies to provide the model and the view. Struts provide a control layer for the web application, which reduces construction time and maintenance costs.

The Tiles framework builds on the jsp:include feature and is bundled with the Struts web application framework. The Tiles framework reduces the duplication between JavaServer Pages (JSP) files and makes website layouts flexible and easy to maintain by assembling presentation pages from component parts.

Struts and Tiles caching is an extension of servlet and JSP caching, so the actions performed for each type of caching are very similar. Refer to the Configuring servlet caching topic for more information about servlet caching.

Procedure

  1. Enable servlet and JSP caching. Enabling servlet caching automatically enables Struts and Tiles caching.
    Refer to the Configuring servlet caching topic for more information about servlet caching.
  2. Develop the cache policy. A cache policy is required to cache a struts or tiles response.
    To develop a Struts cache policy:

    The Struts framework provides the controller component in the MVC-style application. The controller is a servlet called org.apache.struts.action.ActionServlet.class. In the web.xml file of the application, a servlet mapping of *.do is added for this Struts ActionServlet servlet so that every request for a Web address that ends with .do is processed. The ActionServlet servlet uses the information in the struts-config.xml file to decide which Struts action class runs the request for the specified resource.

    In the previous version of WebSphere® Application Server, only one cache policy per servlet was supported. However, when you are using Struts, every request that ends in .do maps to the same ActionServlet servlet. To cache Struts responses, write a cache policy for the ActionServlet servlet based on its servlet path.

    For example, consider two Struts actions: /HelloParam.do and /HelloAttr.do. To cache the responses based on the id request parameter and the arg request attribute respectively, use the following cache policy:
    <cache-entry>
    	<class>servlet</class>
    	<name>org.apache.struts.action.ActionServlet.class</name>
    	<cache-id>
    		<component id="" type="servletpath">
    			<value>/HelloParm.do</value>
    		</component>
    	</cache-id>
    	<cache-id>
    		<component id="" type="servletpath">
    			<value>/HelloAttr.do</value>
    		</component>
    		<component id="arg" type="attribute">
    			<required>true</required>
    		</component>
    	</cache-id>
    </cache-entry>
    
    With the current version of WebSphere Application Server, you can map multiple cache policies for a single servlet. You can rewrite the previous cache policy as in the following example:
    <cache-entry>
    	<class>servlet>
    	<name>/HelloParam.do</name>
    	<cache-id>
    		<component id="id" type="parameter">
    			<required>true</required>
    		</component>
    </cache-entry>
    <cache-entry>
    	<class>servlet</class>
    	<name>/HelloAttr.do</name>
    	<cache-id>
    		<component id="arg" type="attribute">
    			<required>true</required>
    		</component>
    	</cache-id>
    </cache-entry>
    
    To develop a Tiles cache policy:
    The Tiles framework is built on the jsp:include tag, so everything that applies to JSP caching also applies to Tiles. You must set the flush attribute to true in any fragments that are included using the tiles:insert tag for the fragments to be cached correctly. The extra feature in tiles caching over JSP caching is based on the tiles attribute. For example, you might develop the following layout.jsp template:
    <html>
    	<%String categoryId = request.getParameter("categoryId")+"test"; %>
    	<tiles:insert attribute="header">
    		<tiles:put name="categoryId" value="<%= categoryId %>" />
    	</tile:insert>
    	<table>
    		<tr>
    			<td width="70%" valign="top"><tiles:insert attribute="body" /> </td>
    		</tr>
    		<tr>
    			<td colspan="2"><tiles:insert attribute="footer" /></td>
    		</tr>
    	</table>
    </body>
    </html>
    
    The nested tiles:put tag specifies the attribute of the inserted tile. In the layout.jsp template, the categoryId attribute is defined and passed on to the tile that is inserted into the placeholder for the header. In the following example, the layout.jsp file is inserted into another JSP file:
    <html>
    <body>
    <tiles:insert page="layout.jsp?categoryId=1002" flush="true">
    	<tiles:put name="header" value="/header.jsp" />
    	<tiles:put name="body" value="/body.jsp" />
    	<tiles:put name="footer" value="/footer.jsp" />
    </tiles:insert>
    </body>
    </html>
    
    The categoryId tile attribute is passed on to the header.jsp file. The header.jsp file can use the <tiles:useAttribute> tag to retrieve the value of categoryId. To cache the header.jsp file based on the value of the categoryId attribute, you can use the following cache policy:
    <cache-entry>
    	<class>servlet</class>
    	<name>/header.jsp</name>
    	<cache-id>
    		<component id="categoryId" type="tiles_attribute">
    		<required>true</required>
    		</component>
    	</cache-id>
    </cache-entry>
    
  3. Ensure your cache policy is working correctly. You can modify the policies within the cachespec.xml file while your application is running.
    Refer to the Configuring cacheable objects with the cachespec.xml file topic for more information about cache policies.

Results

What to do next

Refer to the Task overview: Using the dynamic cache service to improve performance for more information about the dynamic cache.