1. web.xml配置,加入
<!-- struts2 tiles config -->
<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>
2. struts.xml 配置。
<package name="com.test.action" extends="tiles-default">
<action name="login" class="com.test.action.LoginAction">
<exception-mapping result="usernameInvalid" exception="com.test.exception.UsernameException" />
<exception-mapping result="passwordInvalid" exception="com.test.exception.PasswordException" />
<!--
<result name="success">/jsp/index.jsp</result>
-->
<result name="success" type="tiles">mainpage</result>
<result name="input">/jsp/login.jsp</result>
<result name="usernameInvalid">/jsp/usernameInvalid.jsp</result>
<result name="passwordInvalid">/jsp/passwordInvalid.jsp</result>
</action>
3 新增布局定义文件,WEB-INF/tiles.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
<definition name="homepage" template="/jsp/tiles/layout.jsp">
<put-attribute name="title" value="Tiles tutorial homepage" /> <!--旧版本是put,不是put-attribut-->
<put-attribute name="header" value="/jsp/tiles/header.jsp" />
<put-attribute name="menu" value="/jsp/tiles/menu.jsp" />
<put-attribute name="body" value="/jsp/tiles/cBody.jsp" />
<put-attribute name="footer" value="/jsp/tiles/footer.jsp" />
</definition>
<definition name="mainpage" extends="homepage">
<put-attribute name="body" value="Manual Transaction Input"/>
</definition>
</tiles-definitions>
4 新增TLD文件,WEB-INF/tiles-jsp.tld。在tiles-jsp.jar包中。
5. 模板layout.jsp文件。
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/WEB-INF/tiles-jsp.tld" prefix="tiles"%>
<html>
<head>
<title></title>
</head>
<body>
<table width="100%" height="100%" border="2" align="center">
<tr>
<td colspan="2" align="center" valign="top" width="100%" height="100px" bgcolor="#80ff80">
<tiles:insertAttribute name="header" />
</td>
</tr>
<tr>
<td align="center" width="150px" height="100%" bgcolor="#00ff00">
<tiles:insertAttribute name="menu" />
</td>
<td align="right" width="618px" height="100%" 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>
jar基本包(6个)
1.commons-logging-1.0.4.jar
2.commons-fileupload-1.2.1.jar
3.ognl-2.6.11.jar
4.struts2-core-2.1.6.jar
5.xwork-2.1.2.jar
6.freemarker-2.3.13.jar
整合tiles必须包(7个)
.struts2-tiles-plugin-2.1.6.jar
2.tiles-api-2.0.6.jar
3.tiles-core-2.0.6.jar
4.tiles-jsp-2.0.6.jar
5.commons-digester-1.8.jar
6.commons-collections-3.2.jar
7.commons-beanutils-1.7.0.jar