1.在WEB-INF/lib下加入所需的jar包
commons-digester-1.6.jar,
tiles-core-2.0-20070207.130156-4.jar,
tiles-api-2.0-20070207.130156-4.jar,
struts2-tiles-plugin-2.0.6.jar,
struts2-core-2.0.6.jar
xwork-2.0.1.jar,
2. 以下内容添加到web.xml
<context-param>
<param-name>org.apache.tiles.CONTAINER_FACTORY</param-name>
<param-value>
org.apache.struts2.tiles.StrutsTilesContainerFactory
</param-value>
</context-param>
<context-param>
<param-name> org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG
</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>
<listener>
<listener-class>
org.apache.struts2.tiles.StrutsTilesListener
</listener-class>
</listener>
3.在WEB-INF下添加和tiles.tld和tiles.xml文件,其中tiles.tld内容为tiles-core-2.0-20070207.130156-4.jar包中META_INF/tiles-core.tld的内容。
tiles.xml内容:
<?xml version="1.0" encoding="GB2312" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://jakarta.apache.org/struts/dtds/tiles-config.dtd">
<tiles-definitions>
<definition name="myapp.homepage" template="layout.jsp">
<put name="title" value="Tiles tutorial homepage" />
<put name="header" value="/tiles/header.jsp" />
<put name="menu" value="/tiles/menu.jsp" />
<put name="body" value="/tiles/cBody.jsp" />
<put name="footer" value="/tiles/footer.jsp" />
</definition>
</tiles-definitions>
4.struts.xml 为:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="tiles-default">
<action name="go" class="com.action.MyAction">
<!--result name="success">/next.jsp</result-->
<result name="success" type="tiles">myapp.homepage</result>
</action>
</package>
</struts>
红色部分根据自己项目定。注意extends="tiles-default"
5创建layout.jsp:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="WEB-INF/tiles.tld " prefix="tiles"%>
<html>
<head>
<title></title>
</head>
<body>
<table width="768px" height="800px" border="2" align="center">
<tr>
<td colspan="2" align="center" valign="top" width="768px" height="100px" bgcolor="#80ff80">
<tiles:insertAttribute name="header" />
</td>
</tr>
<tr>
<td align="center" width="150px" height="800px" bgcolor="#00ff00">
<tiles:insertAttribute name="menu" />
</td>
<td align="right" width="618px" height="800px" bgcolor="#ff80c0">
<tiles:insertAttribute name="body" />
</td>
</tr>
<tr>
<td colspan="2" bgcolor="#00ff40" height="100px">
<tiles:insertAttribute name="footer" />
</td>
</tr>
</table>
</body>
</html>
6.根据
<put name="title" value="Tiles tutorial homepage" />
<put name="header" value="/tiles/header.jsp" />
<put name="menu" value="/tiles/menu.jsp" />
<put name="body" value="/tiles/cBody.jsp" />
<put name="footer" value="/tiles/footer.jsp" />
在WebRoot下创建tiles目录和相应jsp文件
另:tiles的展现有点可以让你用更少的代码做更多的事情,也会让你的UI布局管理更轻松;不久之前谈过如何使用struts2,今天谈谈如何将 tiles与struts2集成:
1、安装安装共有两步: a、tiles的安装:将tiles根目录以及根目录下lib子目录中的jar文件放到WEB- INF/lib目录下。 b、struts-tiles-plugin安装:将struts2-tiles-plugin jar文件考本到WEB-INF/ 目录下。
2、web.xml的配置一个配置示例胜过千言万语:
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class> </listener>
<context-param>
<param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name> <param-value>/WEB-INF/config/tiles/tiles-defs.xml</param-value>
</context-param>
3、action的配置使用tiles作为resulttype的action必须扩展tiles-default包或者定义tiles 的resulttype
a、扩展tiles-default的配置:
<package name="index-pkg" extends="struts-default,tiles-default" namespace="/">
<action name="index" class="org.tibetjungle.rips.action.IndexAction" method="index">
<result name="success" type="tiles">mainLayout</result>
</action> </package>
b、在配置文件中自定义:
<result-types> <
result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>