Configure 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. For more information about developing Struts and Tiles applications, see The Apache Struts Web Application Framework.

 

Overview

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 Web site 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. See Servlet caching for more information.

 

Procedure

  1. Enable servlet and JSP caching. Enabling servlet caching automatically enables Struts and Tiles caching. See Configuring servlet caching for more information.

  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.

    Cache policy using a previous version of WebSphere Application Server

    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>
    

    Cache policy using WebSphere Application Server, v6 or later

    With the current version of WebSphere Application Server, one can map multiple cache policies for a single servlet. We 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  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, 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. We can modify the policies within the cachespec.xml file while your application is running. See Configuring cacheable objects with the cachespec.xml file for more information about cache policies.

 

Result

 

What to do next

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