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.
Why and when to perform 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 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.
Steps for this task
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.
<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, Version 6.0 or later
<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>
<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>
Result
What to do next
See Task overview: Using the dynamic cache service to improve performance for more information about the dynamic cache.