struts中使用tiles组件(原创)

本文详细介绍了如何在Struts2项目中集成Tiles框架,包括在struts.xml中配置Tiles插件,创建tiles-defs.xml定义文件,设计布局页面layout.jsp,以及创建各个内容部分的JSP文件。通过配置action-mappings,实现不同页面内容的插入,并在web.xml中引入必要的标签库。按照教程步骤操作,可以在本地环境中成功运行并展示Tiles布局效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在你的struts配置文件中加入下面的配置:
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" value="/WEB-INF/tiles-def.xml" />
<set-property property="definitions-parser-validate" value="true" />
 </plug-in>
2.生成tiles-def.xml文件:
<xml version="1.0" encoding="ISO-8859-1" >
<!DOCTYPE tiles-definitions PUBLIC
 "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
 "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">

<tiles-definitions>
 <definition name="base-definition" path="/layout.jsp">
 <put name="sidebar" value="sidebar.jsp"/>
 <put name="header" value="header.jsp"/>
 <put name="content" value=""/>
 <put name="footer" value="footer.jsp"/>
 </definition>

 <definition name="index-definition" extends="base-definition">
 <put name="content" value="indexContent.jsp"/>
 </definition>

</tiles-definitions>

3.生成layout.jsp布局文件:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<html>
<head> <title>布局设计</title> </head>
<body >
<% One table lays out all of the content %>
<table width="100%" height="100%">
<% Sidebar section %>
<tr>
<td width="150" valign="top" align="left" bgcolor="#CCFFCC">
 <tiles:insert attribute="sidebar"/> </td> <% Main content section %>
<td valign="top" height="100%" width="*"> <table width="100%" height="100%">
<tr> <% Header section %> <td height="15%"> <tiles:insert attribute="header"/>
 </td> <tr> <tr> <% Content section %>
<td valign="top" height="*"> <tiles:insert attribute="content"/>
</td> </tr> <tr> <% Footer section %> <td valign="bottom" height="15%">
<tiles:insert attribute="footer"/> </td> </tr> </table> </td> </tr> </table> </body> </html>
4.生成要使用的JSP文件sidebar.jsp,header.jsp,footer.jsp。

5.通过action-mappings配置你的tiles组件:
<action-mappings>
<action path="/index"
  type="org.apache.struts.actions.ForwardAction"
 parameter="index-definition">
</action>
</action-mappings>
parameter参数的值,是你在tiles-def.xml文件里某个的define的name。

别忘了在web.xml中加入
<taglib>
 <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
 <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
 </taglib>
同时还要保证你使用的是struts 1.1版本。
现在就可以动手为你的项目加入tiles应用了。
完成以上步骤,完成Tomcate部署并启动,通过http://localhost:8080/index.do可以看到下面的效果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值