Spring中整合Titles
在《Spriing实战(第三版)》这本书中,有一个使用titles的例子,但是这是一个不完整的例子。那么要参照起来就比较难了,于是找到了下面这篇博客。
在Spring中使用tiles2 (因为是英文的,同时又是比较简单的英文,那么就翻译一下,当作学习)
在这个例子中,你将学会怎样整合Spring和Tiles2.这个例子的目录结构如下:

添加下面的库文件到库目录,(当然如果是在Eclipse中就是对应的lib文件夹了)。
01.antlr-runtime-3.0 02.commons-logging-1.0.4 03.org.springframework.asm-3.0.0.M3 04.org.springframework.beans-3.0.0.M3 05.org.springframework.context-3.0.0.M3 06.org.springframework.context.support-3.0.0.M3 07.org.springframework.core-3.0.0.M3 08.org.springframework.expression-3.0.0.M3 09.org.springframework.web-3.0.0.M3 10.org.springframework.web.servlet-3.0.0.M3 11.12.commons-beanutils-1.7.0 13.commons-digester-1.8 14.commons-logging-api-1.1 15.jstl 16.standard 17.tiles-api-2.0.4 18.tiles-core-2.0.4 19.tiles-jsp-2.0.4 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="viewResolver" class="org.springframework.web.servlet.view. ResourceBundleViewResolver" p:basename="views" /> <context:component-scan base-package="com.vaannila.web" /> <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2. TilesConfigurer" p:definitions="/WEB-INF/tiles-defs.xml" /> </beans>
使用definitions属性指定Tiles定义文件的位子,这里这个位置是“/WEB-INF/tiles-defs.xml"。Tiles定义文件展示如下:
<?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="baseLayout" template="/WEB-INF/tiles/baseLayout.jsp">
<put-attribute name="title" value="Template"/>
<put-attribute name="header" value="/WEB-INF/tiles/header.jsp"/>
<put-attribute name="menu" value="/WEB-INF/tiles/menu.jsp"/>
<put-attribute name="body" value="/WEB-INF/tiles/body.jsp"/>
<put-attribute name="footer" value="/WEB-INF/tiles/footer.jsp"/>
</definition> <definition name="welcome" extends="baseLayout">
<put-attribute name="title" value="Welcome"/>
<put-attribute name="body" value="/WEB-INF/jsp/welcome.jsp"/>
</definition> <definition name="friends" extends="baseLayout">
<put-attribute name="title" value="Friends"/>
<put-attribute name="body" value="/WEB-INF/jsp/friends.jsp"/>
</definition> <definition name="office" extends="baseLayout">
<put-attribute name="title" value="Office"/>
<put-attribute name="body" value="/WEB-INF/jsp/office.jsp"/>
</definition> </tiles-definitions>
这里我们首先定义了基本的布局,以后我们将扩展这个基本的布局并且将通过仅仅改变标题和主体部分创建更多tiles。
welcome.(class)=org.springframework.web.servlet.view.tiles2.TilesView
welcome.url=welcome friends.(class)=org.springframework.web.servlet.view.tiles2.TilesView
friends.url=friends office.(class)=org.springframework.web.servlet.view.tiles2.TilesView
office.url=office about.(class)=org.springframework.web.servlet.view.JstlView
about.url=/WEB-INF/jsp/about.jsp
welcome, friends 和 office 引用tile 定义的名字 (the one to right side of the = sign)。我们使用 "org.springframework.web.servlet.view.tiles2. TilesView" 类展示tile.你也可以一起使用其他的TilesView。相关的url通过org.springframework.web.servlet.view.JstlView被映射到相关的jsp页面。
baseLayout.jsp文件包含了拥有不同区域的的表结构。
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<!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">
<tiles:insertAttribute name="menu" />
</td>
<td width="350">
<tiles:insertAttribute name="body" />
</td>
</tr>
<tr>
<td height="30" colspan="2">
<tiles:insertAttribute name="footer" />
</td>
</tr>
</table>
</body>
</html>
这里我们使用注解controller处理映射去处理请求。在在redirect.jsp页面我们只是请求welcome.htm。
<% response.sendRedirect("welcome.htm"); %>
我们向前到 welcome.htm的url将通过WelcomeController类处理。
package com.vaannila.web; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class WelcomeController { @RequestMapping("/welcome.htm")
public String redirect()
{
return "welcome";
}
}
运行时显示的界面如下:
Spring中整合Titles的更多相关文章
- spring中整合memcached,以及创建memcache的put和get方法
spring中整合memcached,以及创建memcache的put和get方法: 1:在项目中导入memcache相关的jar包 2:memcache在spring.xml的配置: 代码: < ...
- 玩转Spring MVC(五)----在spring中整合log4j
在前边的基础上,本文主要总结一下如何在spring 中配置log4j,在本文末尾会给出完整项目的链接. 首先是web.xml中要新添加的代码: <!-- 6. 配置log4j --> &l ...
- spring中整合ssm框架注解版
和xml版差不多,只不过创建对象的方式是由spring自动扫描包名,然后命名空间多一行context代码在application.xml中,然后将每个对象通过注解创建和注入: 直接上代码: 1.use ...
- Spring中整合Cage,实现验证码功能
1.pom.xml中添加Cage依赖. <dependency> <groupId>com.github.cage</groupId> <artifactId ...
- Spring框架中整合JUnit单元测试的方法
一. 步骤: 1. 拷贝jar包: 1. JUnit-4.9.jar和spring-test-4.2.4.RELEASE.jar ; 2. 替换原来的main函数: 1. 在测试类上使用注解方式替换: ...
- 让Mongo在Spring中跑起来
本文标题为<让Mongo在Spring中跑起来>,旨在Spring中如何成功连接MongoDB并对其进行增删改查等操作,由于笔者也是刚接触,对其中的一些原由也不甚了解,若有错误之处,敬请指 ...
- Spring中AOP的实现
Spring中整合了AOP的功能,虽然有不足,没有专门做AOP框架的那么完美,但是用一用感觉还是不错的 一些概念: AOP 面向切面编程 aspect 切面/切面类(我个人认为一个真正被解耦的程序,切 ...
- ASP.NET MVC3 中整合 NHibernate3.3、Spring.NET2.0 时 Session 关闭问题
一.问题描述 在向ASP.NET MVC中整合NHibernate.Spring.NET后,如下管理员与角色关系: 类public class Admin { public virtual strin ...
- [转载] 【Shiro】Apache Shiro架构之实际运用(整合到Spring中)
写在前面:前面陆陆续续对Shiro的使用做了一些总结,如题,这篇博文主要是总结一下如何将Shiro运用到实际项目中,也就是将Shiro整到Spring中进行开发.后来想想既然要整,就索性把Spring ...
随机推荐
- 【原】1.1RDD源码解读(一)
1.RDD(Resilient Distributed DataSet)是Spark生态系统中最基本的抽象,代表不可变的.可并行操作的分区元素集合.RDD这个类有RDD系列所有基本的操作,比如map. ...
- 【CSS3】Advanced2:Shadows
1.Box Shadows box-shadow:h-shadow v-shadow [blur模糊距离 spread阴影尺寸 color inset]; 2. Text Shadows text-s ...
- linux find用法总结
前言:我们为什么要学会使用find命令? 转自:http://blog.chinaunix.net/uid-24648486-id-2998767.html 每一种操作系统都有成千上万的文件组成,对于 ...
- HDU 2509
与HDU 1907一样... #include<cstdio> #include<cstring> #include<cstdlib> #include<io ...
- HW3.20
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- 小波变换和motion信号处理(三)(转)
这篇文章算太监了,去作者blog提问去吧:http://www.kunli.info/2012/04/08/fourier-wavelet-motion-signal-3/ 从前两篇发布到现在,过去一 ...
- JavaScript- The Good Parts Chapter 3 Objects
Upon a homely object Love can wink.—William Shakespeare, The Two Gentlemen of Verona The simple type ...
- [四]SpringMvc学习-对servlet与json的支持与实现
1.对servletAPI的支持 request.response.session作为参数自动注入 2.对Json的支持 2.1springmvc配置文件中添加支持对象与json的转换 <mvc ...
- ADODB.Connection 错误 '800a0e7a' 未找到提供程序 该程序可能未正确安装
今天调程序的时候发现的,错误提示如下: ADODB.Connection 错误 '800a0e7a' 未找到提供程序.该程序可能未正确安装. /conn.asp,行 6 因为系统是64位的win7或w ...
- Spring 3.0 注解注入详解
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...