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>
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>