在你的struts配置文件struts
config
xml中加入下面的配置
<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>
生成tiles
def
xml文件
<?xml version=
encoding=
ISO
?>
<!DOCTYPE tiles
definitions PUBLIC
//Apache Software Foundation//DTD Tiles Configuration
//EN
/struts/dtds/tiles
config_
_
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>
生成layout
jsp布局文件
<%@ page contentType=
text/html; charset=GBK
%>
<%@ taglib uri=
/tags/struts
tiles
prefix=
tiles
%>
<html>
<head> <title>布局設計</title> </head>
<body >
<table width=
%
height=
%
>
<tr>
<td width=
valign=
top
align=
left
bgcolor=
#CCFFCC
>
<tiles:insert attribute=
sidebar
/>
</td>
<td valign=
top
height=
%
width=
*
>
<table width=
%
height=
%
>
<tr> <td height=
%
> <tiles:insert attribute=
header
/> </td> </tr>
<tr> <td valign=
top
height=
*
> <tiles:insert attribute=
content
/> </td></tr>
<tr> <td valign=
bottom
height=
%
><tiles:insert attribute=
footer
/></td></tr>
</table>
</td>
</tr>
</table>
</body> </html>
生成要使用的JSP文件sidebar
jsp
header
jsp
footer
jsp
通過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>/tags/struts
tiles</taglib
uri>
<taglib
location>/WEB
INF/struts
tiles
tld</taglib
location>
</taglib>
同時還要保證你使用的是struts
版本
現在就可以動手為你的項目加入tiles應用了
完成以上步驟
完成Tomcate部署並啟動
通過
:
/strutsTiles/index
do可以看到效果
運行下面兩個文件是同樣的效果
index
jsp
<%@ page contentType=
text/html; charset=gb
%>
<%@ taglib uri=
/tags/struts
tiles
prefix=
tiles
%>
使用邏輯名
<tiles:insert definition=
index
definition
/>
index
jsp
<%@ page contentType=
text/html; charset=gb
%>
<%@ taglib uri=
/tags/struts
tiles
prefix=
tiles
%>
<tiles:insert page=
layout
jsp
flush=
true
>
<tiles:put name=
sidebar
value=
sidebar
jsp
/>
<tiles:put name=
header
value=
header
jsp
/>
<tiles:put name=
content
value=
indexContent
jsp
/>
<tiles:put name=
footer
value=
footer
jsp
/>
</tiles:insert>
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28108.html