Tiles的使用
Tiles 是apache推出的布局管理工具包。其原理和功能类似于jsp:include 和 <c:import >标签。如果多个页面中有多个公共的内容时,使用 Tiles来定义一个公有区域的模板,在其它页面中只需要简单引入就可以了。
安装步骤:
1. 在
http://struts.apache.org/ 上下载 struts 。解压缩后将
a) struts.jar
b) commons-beanutils.jar
c) commons-collections.jar
d) commons-digester.jar
e) commons-logging.jar
放入包lib包中。
2. 在 web.xml中配置
<
servlet
>
<
servlet-name
>
Tiles Servlet
</
servlet-name
>
<
servlet-class
>
org.apache.struts.tiles.TilesServlet
</
servlet-class
>
<
init-param
>
<
param-name
>
definitions-config
</
param-name
>
<
param-value
>
/WEB-INF/tiles.xml
</
param-value
>
</
init-param
>
<
load-on-startup
>
2
</
load-on-startup
>
</
servlet
>
3. 在web-inf/目录下新建 tiles.xml 文件
<!
DOCTYPE
tiles-definitions
PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration//EN"
"http://jakarta.apache.org/struts/dtds/tiles-config.dtd"
>
<
tiles-definitions
>
<
definition
name
=
"subview"
path
=
"/pages/subview.jsp"
>
<
put
name
=
"jianshe"
value
=
"jianshetest"
></
put
>
</
definition
>
</
tiles-definitions
>
定义布局
4.
subview.jsp
的代码:
<%@
page
language
=
"java"
contentType
=
"text/html; charset=UTF-8"
pageEncoding
=
"ISO-8859-1"
%>
<%@
taglib
uri
=
"http://java.sun.com/jsf/core"
prefix
=
"f"
%>
<%@
taglib
uri
=
"http://java.sun.com/jsf/html"
prefix
=
"h"
%>
<%@
taglib
uri
=
"http://jakarta.apache.org/struts/tags-tiles"
prefix
=
"tiles"
%>
<
f:subview
id
=
"sub"
>
<
tiles:importAttribute
scope
=
"request"
/>
<
h:outputText
value
=
"asfdasfdasfasfasfasf"
></
h:outputText
>
<
h:outputText
value
=
"#{jianshe}"></h:outputText>
</
f:subview
>
5.
index.jsp
的代码
<%@
page
language
=
"java"
contentType
=
"text/html; charset=ISO-8859-1"
pageEncoding
=
"ISO-8859-1"
%>
<%@
taglib
uri
=
"http://java.sun.com/jsf/core"
prefix
=
"f"
%>
<%@
taglib
uri
=
"http://java.sun.com/jsf/html"
prefix
=
"h"
%>
<%@
taglib
uri
=
"http://jakarta.apache.org/struts/tags-tiles"
prefix
=
"tiles"
%>
<!
DOCTYPE
HTML
PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
>
<
html
>
<
head
>
<
title
>
Insert title here
</
title
>
</
head
>
<
body
>
<
f:view
>
<%--
<jsp:include flush="true" page="subview.jsp"></jsp:include>
--%>
<
tiles:insert
definition
=
"subview"
flush
=
"false"
></
tiles:insert
>
</
f:view
>
</
body
>
</
html
>
注意:
1. tils.xml中可以传给模板传参数:
<
put
name
=
"jianshe"
value
=
"jianshetest"
></
put
>
定义名为
jianshe
变量
在
subview.jsp
中引用为
<
h:outputText
value
=
"#{jianshe}"></h:outputText>
在使用变量前需要加入
<
tiles:importAttribute
scope
=
"request"
/>
来导入变量
stieMesh是也是一种布局管理工具,原理是对输出结果过滤产生 符合统一布局的页面效果:
有关使用请参阅: