Struts Tiles 是一个页面模板引擎,属于视图层。定义页面布局,不指定里面的内容,当web页面运行时,才把特定的jsp内容插入到模板中显示。
该案例涉及到的file:web.xml,struts-default.xml,tiles-defs.xml,sidepanel.jsp, welcome.jsp
1. Web.xml
<!--Action Servlet Configuration -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-default.xml</param-value>
</init-param>
<init-param>
<param-name>config/acctmgmt</param-name>
<param-value>/WEB-INF/struts-acctmgmt.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
2. Struts-default.xml
引入tiles-defs.xml文件
<plug-inclassName="org.apache.struts.tiles.TilesPlugin" >
<set-propertyproperty="definitions-config"
value="/WEB-INF/tiles-defs.xml"/>
<set-propertyproperty="moduleAware" value="false" />
</plug-in>
3. tiles-defs.xml
定义tiels,创建tiles组件,配置模板资源
<tiles-definitions>
<!--site layout -->
<definitionname="siteLayout" path="/sitelayout.jsp">
<putname="header" value="header.jsp" />
<putname="main" value="mainLayout" />
<putname="footer" value="footer.jsp" />
</definition>
<!--layout of main area -->
<definitionname="mainLayout" path="mainlayout.jsp">
<putname="sidepanel" value="sideLayout" />
<putname="content" value="contents" />
</definition>
<!--layout of sidepane -->
<definitionname="sideLayout" path="sidepanel.jsp">
<putname="leftupper"value="../upper.jsp" />
<putname="leftlower"value="../lower.jsp" />
</definition>
<!--layout of content area -->
<definitionname="contents" path="contents.jsp">
<putname="section1" value="" type="string" />
<putname="section2" value="" type="string" />
<putname="section3" value="" type="string" />
<putname="section4" value="" type="string" />
<putname="section5" value="" type="string" />
<putname="section6" value="" type="string" />
</definition>
</tiles-definitions>
4. sidepanel.jsp
引用tiles,创建模板布局
<%@ tagliburi="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<table role="presentation"cellpadding="2" cellspacing="0">
<tr>
<tdvalign="top">
<table role="presentation">
<tiles:insert attribute="leftupper"ignore="true"/>
<tr>
<td>
<div>
<tiles:insert attribute="leftlower" ignore="true"/>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
5. welcome.jsp
在jsp页面中使用tiles
<%@ tagliburi="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<tiles:insert definition="siteLayout"flush="true">
<tiles:put name="pageTitle" type="string"value="Site Entrance" />
<tiles:put name="pageHeader"type="string" value="" />
<tiles:put name="content"type="string">
<!-- page content goes here -->
</tiles:put>
</tiles:insert>
本文出自 “技术之旅” 博客,请务必保留此出处http://faraway.blog.51cto.com/4628383/1736576
原文地址:http://faraway.blog.51cto.com/4628383/1736576