1. 添加Tiles的jar依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<
dependency
>
<
groupId
>org.apache.tiles</
groupId
>
<
artifactId
>tiles-api</
artifactId
>
<
version
>3.0.4</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.apache.tiles</
groupId
>
<
artifactId
>tiles-core</
artifactId
>
<
version
>3.0.4</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.apache.tiles</
groupId
>
<
artifactId
>tiles-jsp</
artifactId
>
<
version
>3.0.4</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.apache.tiles</
groupId
>
<
artifactId
>tiles-el</
artifactId
>
<
version
>3.0.4</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.apache.tiles</
groupId
>
<
artifactId
>tiles-extras</
artifactId
>
<
version
>3.0.4</
version
>
</
dependency
>
<
dependency
>
<
groupId
>org.apache.tiles</
groupId
>
<
artifactId
>tiles-servlet</
artifactId
>
<
version
>3.0.4</
version
>
</
dependency
>
|
2. 配置web.xml,使用Tiles监听器,配置加载tiles容器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
id
=
"pum"
version
=
"2.4"
xmlns
=
"http://java.sun.com/xml/ns/j2ee"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
=
"http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
<
display-name
>Archetype Created Web Application</
display-name
>
<
context-param
>
<
param-name
>
org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</
param-name
>
<
param-value
>
/WEB-INF/tiles.xml
</
param-value
>
</
context-param
>
<
listener
>
<
listener-class
>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</
listener-class
>
</
listener
>
<
welcome-file-list
>
<
welcome-file
>index.jsp</
welcome-file
>
</
welcome-file-list
>
</
web-app
>
|
3. 定义模板的结构,并使用JSP表现这个结构。
3.1 模板的结构
3.2 创建HelloTiles.jsp文件
1
2
|
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<
tiles:insertDefinition
name
=
"myapp.homepage"
/>
|
4. 在web下创建tiles.xml,定义Tiles模板
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<
tiles-definitions
>
<
definition
name
=
"myapp.homepage"
template
=
"/WEB-INF/layouts/classic.jsp"
>
<
put-attribute
name
=
"title"
value
=
"Tiles tutorial homepage"
/>
<
put-attribute
name
=
"header"
value
=
"/WEB-INF/tiles/banner.jsp"
/>
<
put-attribute
name
=
"menu"
value
=
"/WEB-INF/tiles/common_menu.jsp"
/>
<
put-attribute
name
=
"body"
value
=
"/WEB-INF/tiles/home_body.jsp"
/>
<
put-attribute
name
=
"footer"
value
=
"/WEB-INF/tiles/credits.jsp"
/>
</
definition
>
</
tiles-definitions
>
|
5. 项目结构图
6. 源码下载地址:http://pan.baidu.com/s/1eQ5q3QU
本文转自 genuinecx 51CTO博客,原文链接:http://blog.51cto.com/favccxx/1555193,如需转载请自行联系原作者