原文地址:原文地址
一.基本包(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个)
1.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
注意:一些文章认为,commons-fileupload-1.2.1.jar、commons-beanutils
-1.7.0.jar这两个包是必须的。
三、添加Struts 2 Tiles Libraries
![Struts2+Tiles2框架整合 - 游目骋怀 - 游目骋怀](http://img1.ph.126.net/v3VgwzoMaSnzDv7yN4pmUw==/2864570838001532571.jpg)
四、配置Web.xml
<!-- tiles2.0 --> <listener> <listener-class> org.apache.struts2.tiles.StrutsTilesListener </listener-class> </listener>
<!-- Struts2 --> …… |
五、创建模板页layout.jsp
<%@ page language="java" pageEncoding="GBK"%> <%@ taglib prefix="s" uri="/struts-tags" %> <%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <base href="<%=basePath%>"> <style> #container{margin-left:auto;margin-right:auto;width:800px;} #header{float:left;width:800px;height:150px;background:red} #daohang{float:left;width:150px;height:400px;background:blue;word-wrap:break-word} #right {float:right;width:642px;height:400px;background:green} #mbottom{float:left;width:800px;height:150px;background:yellow} </style> <div id="container" style="height:auto"> <div id="header"> <tiles:insertAttribute name="header" /> </div>
<div id="daohang"> <tiles:insertAttribute name="left" /> </div>
<div id="right" style="border:none"> <tiles:insertAttribute name="main" /> </div>
<div id="mbottom" style="height:100px"> <tiles:insertAttribute name="foot" /> </div>
</div> |
六、在WEB-INF文件夹中添加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-def" template="/tiles/layout.jsp"> <put-attribute name="header" value="/tiles/11.jsp" /> <put-attribute name="left" value="/tiles/22.jsp" /> <put-attribute name="main" value="/tiles/33.jsp" /> <put-attribute name="foot" value="/tiles/44.jsp" /> </definition> </tiles-definitions> |
注意:
tile1.x的目录是
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE component-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN" "tiles-config_1_1.dtd" > <component-definitions> <definition name="index-def" path="/tiles/layout.jsp"> <put name="header" value="/tiles/11.jsp" /> …… </definition> </component-definitions> |
Tiles2.x的目录为:
<?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-def" template="/tiles/layout.jsp"> <put-attribute name="header" value="/tiles/11.jsp" /> …… </definition> </tiles-definitions> |
七、测试效果
1)通过action测试
applicationContext-action.xml
<bean id="tilesTest" class="cml.test.TilesTest" /> |
Struts.xml
<package name="default" extends="tiles-default"> <action name="tilesTest" class="tilesTest" > <result name="success" type="tiles">index-def</result> </action> </package> |
注意:1、 extends="tiles-default"
2、type="tiles"
2)通过JSP页面直接测试
Mytiles.jsp
<%@ page language="java" pageEncoding="GBK"%> <%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %> <tiles:insertDefinition name="index-def" /> |
注意:与tile1.x不同,tile1.x的是<tiles:insert definition name="index-def" />,
Tile2.x的是<tiles:insertDefinition name="index-def" />