首页 > 代码库 > Springmvc整合tiles框架简单入门示例(maven)

Springmvc整合tiles框架简单入门示例(maven)

Springmvc整合tiles框架简单入门示例(maven)

本教程基于Springmvc,spring mvc和maven怎么弄就不具体说了,这边就只简单说tiles框架的整合。

先贴上源码(免积分下载): 
http://download.csdn.net/detail/zhangbing2434/9435460(这里用的是Idea,eclipse,导入的时候可能会有些差异)

1、tiles依赖的jar包: 
技术分享 
技术分享 
技术分享 
技术分享 
maven代码:

<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>
  • 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
  • 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

2、Spring mvc 中配置Tiles框架(springmvc-servlet.xml)

<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3、配置tiles (tiles.xml)

<?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=http://www.mamicode.com/"" />
           <put-attribute name="header" value=http://www.mamicode.com/"/jsp/head.jsp" />
            <put-attribute name="menu" value=http://www.mamicode.com/"/jsp/menu.jsp" />
            <put-attribute name="body" value=http://www.mamicode.com/"" />
            <put-attribute name="footer" value=http://www.mamicode.com/"/jsp/foot.jsp" />
    </definition>
    <definition name="CustomerForm" extends="base.definition">
        <put-attribute name="title" value=http://www.mamicode.com/"HHHHHHH"/>
        <put-attribute name="body" value=http://www.mamicode.com/"/jsp/CustomerForm.jsp"/>
    </definition>
    <definition name="CustomerDetail" extends="base.definition">
        <put-attribute name="title" value=http://www.mamicode.com/"DDDDDD"/>
        <put-attribute name="body" value=http://www.mamicode.com/"/jsp/CustomerDetail.jsp"/>
    </definition>
</tiles-definitions>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
其中templet.jsp(base.definition)作为模板,其中定义的header,menu,body,footer都需要自己配置相应的jsp文件,一般body为可变的,其他为固定的jsp。
templet.jsp代码:
<%@ 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.mamicode.com/"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.mamicode.com/"http://www.qlysou.com/">www.qlysou.com</a> </p></span>
  • 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
  • 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
   4.效果 

技术分享

写的不是特别好,大家可以下载源码跑起来了看看就明白了,有问题欢迎留言交流

Springmvc整合tiles框架简单入门示例(maven)