yixiaoqi00 2010-10-20
搭建Struts2 与 Tiles 框架整合示例
环境:MyEclipse 8.5 + TOMCAT6 +JDK1.6
一、新建web工程。例如:s2tile2。
二、使用向导添加Struts2.1功能:
引入Struts 2 Core Libraries和Struts 2 Tiles Libraries两个类库包。三、新建s2tile2\WebRoot\index.jsp文件,内容如下:
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<body>
<ahref="go.action">test</a>
</body>
</html>四、修改s2tile2\WebRoot\WEB-INF\web.xml文件,内容如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.5"xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list><listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
<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>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>五、修改s2tile2\src\struts.xml,内容如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<packagename="default"extends="tiles-default">
<actionname="go"class="com.action.MyAction">
<!--resultname="success">/next.jsp</result-->
<resultname="success"type="tiles">myapp.homepage</result>
</action>
</package>
</struts>六、建立s2tile2\src\com\action\MyAction.java类,内容如下:
packagecom.action;
importcom.opensymphony.xwork2.ActionSupport;
publicclassMyActionextendsActionSupport{
publicStringexecute()throwsException{
super.execute();
System.out.println("MyActionexecutesomething...");
returnSUCCESS;
}
}七、新建s2tile2\WebRoot\WEB-INF\tiles.xml,内容如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEtiles-definitionsPUBLIC"-//ApacheSoftwareFoundation//DTDTilesConfiguration2.0//EN""http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definitionname="myapp.homepage"template="layout.jsp">
<put-attributename="title"value="Tilestutorialhomepage"/>
<put-attributename="header"value="/tiles/header.jsp"/>
<put-attributename="menu"value="/tiles/menu.jsp"/>
<put-attributename="body"value="/tiles/cBody.jsp"/>
<put-attributename="footer"value="/tiles/footer.jsp"/>
</definition>
</tiles-definitions>八、从Struts 2 Tiles Libraries中的tiles-jsp-2.0.6.jar包中的META-INF\tld中的tiles-jsp.tld文件释放到s2tile2\WebRoot\WEB-INF\tiles-jsp.tld目录下。
九、新建s2tile2\WebRoot\layout.jsp文件,内容如下:
<%@pagecontentType="text/html;charset=UTF-8"%>
<%@tagliburi="/WEB-INF/tiles-jsp.tld"prefix="tiles"%>
<html>
<head>
<title></title>
</head>
<body>
<tablewidth="768px"height="800px"border="2"align="center">
<tr>
<tdcolspan="2"align="center"valign="top"width="768px"height="100px"bgcolor="#80ff80">
<tiles:insertAttributename="header"/>
</td>
</tr>
<tr>
<tdalign="center"width="150px"height="800px"bgcolor="#00ff00">
<tiles:insertAttributename="menu"/>
</td>
<tdalign="right"width="618px"height="800px"bgcolor="#ff80c0">
<tiles:insertAttributename="body"/>
</td>
</tr>
<tr>
<tdcolspan="2"bgcolor="#00ff40"height="100px">
<tiles:insertAttributename="footer"/>
</td>
</tr>
</table>
</body>
</html>标签说明:
1、<tiles:insertAttribute name="body" />:返回"body"对应的页面的内容信息。
2、<tiles:getAsString name="body" />:返回"body"对应的值,即路径信息。
十、依次新建下列文件:
s2tile2\WebRoot\tiles\cBody.jsp、
s2tile2\WebRoot\tiles\footer.jsp、
s2tile2\WebRoot\tiles\header.jsp、
s2tile2\WebRoot\tiles\menu.jsp,
以上文件内容任意,仅用于测试。十一、发布测试:OK。