Spring3MVC:tiles plugin Tutorial with Example in Eclipse

为了配置tiles,作为TIles框架的入口TilesConfigure必须在spring-servlet.xml中配置,打开spring-servlet.xml从WEB-INF的目录中,添加下面的代码在

<beans></beans>之间

File: /WebContent/WEB-INF/spring-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 >
这个输入配置文件/WEB-INF/tiles.xml作为参数传递在上面的bean定义,这个文件包含了Tiles的定义为我们的应用程序。

创建tiles.xml文件在WEB-INF的目录下,拷贝下面的代码

File: WebContent/WEB-INF/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 = "/WEB-INF/jsp/layout.jsp" >
         < put-attribute name = "title" value = "" />
         < put-attribute name = "header" value = "/WEB-INF/jsp/header.jsp" />
         < put-attribute name = "menu" value = "/WEB-INF/jsp/menu.jsp" />
         < put-attribute name = "body" value = "" />
         < put-attribute name = "footer" value = "/WEB-INF/jsp/footer.jsp" />
     </ definition >
 
     < definition name = "contact" extends = "base.definition" >
         < put-attribute name = "title" value = "Contact Manager" />
         < put-attribute name = "body" value = "/WEB-INF/jsp/contact.jsp" />
     </ definition >
 
</ tiles-definitions >
在tiles.xml中,我们定义了模版base.definition,这个布局包括属性,例如header,title,body,Menu and footer,这个布局然后被继承,

定义了新的contact,我们已经重写了默认的布局,改变了网页题和标题的内容


我们定义了一个模版为我们的应用程序,叫做layout.jsp的jsp文件,这个模版包含了不同的网页的部分,(header,footer,Menu),创建四个页面

layout.jsp,header.jsp,menu.jsp,footer.jsp拷贝下面的内容为每个页面。


File: WebContent/WEB-INF/jsp/layout.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 >

File: WebContent/WEB-INF/jsp/header.jsp

<h1>Header</h1>

File: WebContent/WEB-INF/jsp/menu.jsp

<p>Menu</p>

File: WebContent/WEB-INF/jsp/footer.jsp

<p>Copyright &copy; ViralPatel.net</p>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值