前言
本教程基于springmvc,spring mvc是当前最优秀的mvc框架,自从spring 2.5版本发布后,由于支持注解配置,易用性有了大幅度的提高。spring 3.0更加完善,实现了对struts 2的超越。现在越来越多的开发团队选择了spring mvc。
tiles 框架彻底揭示了 jsp:includes 内部的概念 ―― 从而允许您更灵活地创建可重用的页面。使用 tiles 框架,开发人员能够通过组合可重用的 tile 来构建页面。您应该将 tile 看作是可视组件。
下面本文就给大家简单说tiles框架的整合。
先贴上源码:
springmc.rar(这里用的是idea,eclipse,导入的时候可能会有些差异)
1、tiles依赖的jar包:
maven代码:
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<dependency>
<groupid>commons-beanutils</groupid>
<artifactid>commons-beanutils-core</artifactid>
<version> 1.8 . 3 </version>
</dependency>
<dependency>
<groupid>commons-digester</groupid>
<artifactid>commons-digester</artifactid>
<version> 2.0 </version>
</dependency>
<dependency>
<groupid>log4j</groupid>
<artifactid>log4j</artifactid>
<version> 1.2 . 15 </version>
</dependency>
<dependency>
<groupid>org.slf4j</groupid>
<artifactid>slf4j-api</artifactid>
<version> 1.5 . 8 </version>
</dependency>
<dependency>
<groupid>org.slf4j</groupid>
<artifactid>slf4j-log4j12</artifactid>
<version> 1.4 . 2 </version>
</dependency>
<dependency>
<groupid>org.apache.tiles</groupid>
<artifactid>tiles-api</artifactid>
<version> 2.2 . 1 </version>
</dependency>
<dependency>
<groupid>org.apache.tiles</groupid>
<artifactid>tiles-core</artifactid>
<version> 2.2 . 1 </version>
</dependency>
<dependency>
<groupid>org.apache.tiles</groupid>
<artifactid>tiles-jsp</artifactid>
<version> 2.2 . 1 </version>
</dependency>
<dependency>
<groupid>org.apache.tiles</groupid>
<artifactid>tiles-servlet</artifactid>
<version> 2.2 . 1 </version>
</dependency>
<dependency>
<groupid>org.apache.tiles</groupid>
<artifactid>tiles-template</artifactid>
<version> 2.2 . 1 </version>
</dependency>
|
2、spring mvc 中配置tiles框架(springmvc-servlet.xml)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<bean id= "viewresolver" class = "org.springframework.web.servlet.view.urlbasedviewresolver" >
<property name= "viewclass" >
<value>
org.springframework.web.servlet.view.tiles2.tilesview
</value>
</property>
</bean>
<bean id= "tilesconfigurer" class = "org.springframework.web.servlet.view.tiles2.tilesconfigurer" >
<property name= "definitions" >
<list>
<value>/web-inf/tiles.xml</value>
</list>
</property>
</bean>
|
3、配置tiles (tiles.xml)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?xml version= "1.0" encoding= "utf-8" ?>
<!doctype tiles-definitions public
"-//apache software foundation//dtd tiles configuration 2.0//en"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd" >
<tiles-definitions>
<definition name= "base.definition" template= "/jsp/templet.jsp" >
<put-attribute name= "title" value= "" />
<put-attribute name= "header" value= "/jsp/head.jsp" />
<put-attribute name= "menu" value= "/jsp/menu.jsp" />
<put-attribute name= "body" value= "" />
<put-attribute name= "footer" value= "/jsp/foot.jsp" />
</definition>
<definition name= "customerform" extends = "base.definition" >
<put-attribute name= "title" value= "hhhhhhh" />
<put-attribute name= "body" value= "/jsp/customerform.jsp" />
</definition>
<definition name= "customerdetail" extends = "base.definition" >
<put-attribute name= "title" value= "dddddd" />
<put-attribute name= "body" value= "/jsp/customerdetail.jsp" />
</definition>
</tiles-definitions>
|
其中templet.jsp(base.definition)作为模板,其中定义的header,menu,body,footer都需要自己配置相应的jsp文件,一般body为可变的,其他为固定的jsp。
templet.jsp代码:
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
|
<%@ taglib uri= "http://tiles.apache.org/tags-tiles" prefix= "tiles" %>
<%@ page language= "java" pageencoding= "utf-8" %>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"
"http://www.w3.org/tr/html4/loose.dtd" >
<html>
<head>
<meta http-equiv= "content-type" content= "text/html; charset=utf-8" >
<title><tiles:insertattribute name= "title" ignore= "true" /></title>
</head>
<body>
<table border= "1" cellpadding= "2" cellspacing= "2" align= "center" >
<tr>
<td height= "30" colspan= "2" ><tiles:insertattribute name= "header" />
</td>
</tr>
<tr>
<td height= "250" width= "200" ><tiles:insertattribute name= "menu" /></td>
<td width= "600" ><tiles:insertattribute name= "body" /></td>
</tr>
<tr>
<td height= "30" colspan= "2" ><tiles:insertattribute name= "footer" />
</td>
</tr>
</table>
</body>
</html>
<h1>bottom</h1>
<span style= "font-size: 14px;" ><p><a href= "http://www.qlysou.com/" >www.qlysou.com</a></p></span>
<span style= "font-size: 14px;" ><p>copyright <code class = "xml plain" >©</code><a href= "http://www.qlysou.com/" >www.qlysou.com</a> </p></span>
|
4.效果
写的不是特别好,大家可以下载源码跑起来了看看就明白了,有问题欢迎留言交流
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。
原文链接:http://blog.csdn.net/zhangbing2434/article/details/50686212