为 Struts 和 Tiles 应用程序配置高速缓存

请使用此任务来高速缓存 Struts 和 Tiles 应用程序。

开始之前

在配置 Struts 和 Tiles 高速缓存之前,应该具有已开发的应用程序。您可以在 Apache Struts Web Application Framework 的 Web 站点上找到有关开发 Struts 和 Tiles 应用程序的更多信息。

关于此任务

当需要高速缓存 Struts 和 Tiles 应用程序中的数据时,请使用此任务。

Struts 是一个开放式源代码框架,用于构建使用“模型 - 视图 - 控制器”(MVC) 体系结构的 Web 应用程序。Struts 框架具有控制器组件并且结合了其他技术以提供模型和视图。Struts 为 Web 应用程序提供了控制层,这会减少构造时间和维护成本。

Tiles 框架构建于 jsp:include 功能部件上并与 Struts Web 应用程序框架捆绑在一起。Tiles 框架减少了 JavaServer Pages (JSP) 文件之间的重复,而且通过从组件各部分组装表示页面使 Web 站点布局维护起来很灵活和方便。

Struts 和 Tiles 高速缓存是 Servlet 和 JSP 高速缓存的扩展,因此,为每种类型的高速缓存执行的操作都很相似。有关 Servlet 高速缓存的更多信息,请参阅“配置 Servlet 高速缓存”主题。

过程

  1. 启用 Servlet 和 JSP 高速缓存。启用 Servlet 高速缓存会自动启用 Struts 和 Tiles 高速缓存。 有关 Servlet 高速缓存的更多信息,请参阅“配置 Servlet 高速缓存”主题。
  2. 部署高速缓存策略。高速缓存策略是对 struts 或 tiles 响应进行高速缓存所必需的。
    建立 Struts 高速缓存策略:

    Struts 框架提供了 MVC 样式应用程序中的控制器组件。该控制器是称为 org.apache.struts.action.ActionServlet.class 的 Servlet。在应用程序的 web.xml 文件中,为此 Struts ActionServlet Servlet 添加了 *.do 的 Servlet 映射,以便处理对以 .do 结束的 Web 地址的每个请求。ActionServlet Servlet 使用 struts-config.xml 文件中的信息来决定哪个 Struts 操作类运行对指定资源的请求。

    在先前版本的 WebSphere® Application Server 中,对于每个 Servlet,只支持一个高速缓存策略。但是,当使用 Struts 时,以 .do 结束的每个请求映射至同一 ActionServlet Servlet。要对 Struts 响应进行高速缓存,请根据 ActionServlet Servlet 的 Servlet 路径为它编写高速缓存策略。

    例如,请考虑两个 Struts 操作:/HelloParam.do/HelloAttr.do。要分别对基于 id 请求参数的响应和 arg 请求属性进行高速缓存,请使用以下高速缓存策略:
    <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>
    对于当前版本的 WebSphere Application Server 而言,可以为单个 Servlet 映射多个高速缓存策略。可以按以下示例中所示重新编写先前的高速缓存策略:
    <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>
    建立 Tiles 高速缓存策略:
    Tiles 框架构建于 jsp:include 标记上,因此适用于 JSP 高速缓存的所有内容也适用于 Tiles。必须在使用 tiles:insert 标记包括的任何片段中将 flush 属性设置为 true 才能正确地高速缓存这些片段。基于 JSP 高速缓存的 tiles 高速缓存中的额外功能基于 tiles 属性。例如,可开发以下 layout.jsp 模板:
    <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>
    嵌套的 tiles:put 标记指定已插入 tile 的属性。在 layout.jsp 模板中,定义了 categoryId 属性并将它传递给已插入到头占位符中的 tile。在以下示例中,将 layout.jsp 文件插入到另一 JSP 文件中:
    <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>
    将 categoryId tile 属性传递给 header.jsp 文件。header.jsp 文件可以使用 <tiles:useAttribute> 标记来检索 categoryId 的值。要根据 categoryId 属性的值高速缓存 header.jsp 文件,可以使用以下高速缓存策略:
    <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. 确保您的高速缓存策略正常工作。当您的应用程序正在运行时,您可以在 cachespec.xml 文件中修改策略。 有关高速缓存策略的更多信息,请参阅“使用 cachespec.xml 文件来配置可高速缓存的对象”主题。

结果

下一步做什么

有关动态高速缓存的更多信息,请参阅“任务概述:使用动态高速缓存服务来提高性能”。

指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=tdyn_strutstiles
文件名:tdyn_strutstiles.html