1、新建一工程S2T,导入以下jar包:
commons-beanutils-1.7.0.jar
commons-collections-3.2.jar
commons-digester-1.8.jar
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
freemarker-2.3.13.jar
ognl-2.6.11.jar
struts2-core-2.1.6.jar
struts2-tiles-plugin-2.1.6.jar
tiles-api-2.0.6.jar
tiles-core-2.0.6.jar
tiles-jsp-2.0.6.jar
xwork-2.1.2.jar
2、web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"> <!-- struts2 --> <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>/*</url-pattern> </filter-mapping> <!-- tiles --> <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> </web-app>
3、tiles.xml配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" "http://tiles.apache.org/dtds/tiles-config_2_0.dtd"> <tiles-definitions> <definition name="index" template="/index.jsp"> <put-attribute name="title" value="Home Page" /> <put-attribute name="header" value="/tiles/header.jsp" /> <put-attribute name="menubar" value="/tiles/menubar.jsp" /> <put-attribute name="sidebar" value="/tiles/sidebar.jsp" /> <put-attribute name="content" value="/tiles/content.jsp" /> <put-attribute name="footer" value="/tiles/footer.jsp" /> </definition> <definition name="home" extends="index"> <put-attribute name="content" value="/tiles/body.jsp" /> </definition> </tiles-definitions>
4、struts.xml配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.action.extension" value="do,action" /> <package name="tiles" namespace="/tiles" extends="tiles-default"> <action name="index" class="jp.com.syspro.action.TilesAction" method="execute"> <result name="success" type="tiles">index</result> </action> <action name="home" class="jp.com.syspro.action.TilesAction" method="execute"> <result name="success" type="tiles">home</result> </action> </package> </struts>
5、新建以下JSP文件:
header.jsp
<%@ page pageEncoding="UTF-8"%> <p>Header</p>
menubar.jsp
<%@ page pageEncoding="UTF-8"%> <p>Menu</p>
sidebar.jsp
<%@ page pageEncoding="UTF-8"%> <p>Sidebar</p>
content.jsp
<%@ page pageEncoding="UTF-8"%> <p>Content</p><p>Content</p>
body.jsp
<%@ page pageEncoding="UTF-8"%> <p>Body</p><p>Body</p>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href="../css/main.css"> <title><tiles:insertAttribute name="title" /></title> </head> <body> <div id="container"> <div id="header"> <tiles:insertAttribute name="header" /> </div> <div id="menubar"> <tiles:insertAttribute name="menubar"/> </div> <div id="main"> <div id="sidebar"><tiles:insertAttribute name="sidebar" /></div> <div id="content"><tiles:insertAttribute name="content" /></div> </div> <div id="footer"><tiles:insertAttribute name="footer" /></div> </div> </body> </html>