Tiles的使用方法

1.先建立所需文档(body.jsp header.jsp,fooder.jsp)
2.建立模板文档IndexLayout.jsp


<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title><tiles:getAsString name="title"/></title>

</head>

<body>
<center>
<table width="340" height="284" border="1">
<tr>
<td><tiles:insert attribute="header"/></td>
</tr>
<tr>
<td><tiles:insert attribute="body"/></td>
</tr>
<tr>
<td><tiles:insert attribute="fooder"/></td>
</tr>
</table>
</center>
</body>
</html>

3.编写tiles配制文档tiles-defs.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
<!-- Definitions for Tiles -->
<tiles-definitions>
<definition name="test.indexLayout" path="/layouts/IndexLayout.jsp">
<put name="title" value="Struts-tiles Test"/>
<put name="header" value="/jspages/header.jsp"/>
<put name="body" value="/jspages/body.jsp"/>
<put name="fooder" value="/jspages/fooder.jsp"/>
</definition>
</tiles-definitions>

4.编写struts-config.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
<data-sources />
<form-beans />
<global-exceptions />
<global-forwards />
<action-mappings />
<message-resources parameter="ort.yr.struts.ApplicationResources" />
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml"/>
<set-property property="moduleAware" value="true"/>
</plug-in>
</struts-config>

5.编写使用页index.jsp


<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<tiles:insert definition="test.indexLayout" flush="true"></tiles:insert>


[url]http://hi.baidu.com/lyq32/blog/item/d98bad8f17252beaf01f36af.html[/url]

----------------------------------------------------------------------------------
另外一种布局方法是用siteMesh
使用Sitemesh


Sitemesh是www.opensymphony.org带来的另一款优秀的页面布局工具。它不专门针对Struts ,甚至其它语言的web程序也可以使用它。
它使用Decorator模式达到预期效果。这里可以将页面分为两类,decorator(修饰)和decoratored(被修饰)。这就好比有一个相框和各种不同可以用来变换的相片,当相框中放入不同的相片,就得到不同的视觉效果。
1.下载并安装Sitemesh。
从www.opensymphony.org下载最新的sitemesh 2.3,解压到硬盘。
将<sitemesh>/lib下的common-collections.jar,freemarker.jar, velocity-deps-1.3.1.jar,velocity-tools-view-1.3.1.jar和<sitemesh>/dist下sitemesh-2.3.jar添加到项目Libraries中。
将<sitemesh>\src\etc\tld\jsp1.2的sitemesh-decorator.tld和sitemesh-page.tld复制一份到项目的web/WEB-INF/下面。
2.配置sitemesh。
从configurations中打开web.xml,定义Sitemesh Filter。
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>
com.opensymphony.module.sitemesh.filter.PageFilter
 </filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


在下面的jsp-config中添加sitemesh的taglib定义,在最新的jsp 2.0标准,这不是必须的。
<taglib>
<taglib-uri>sitemesh-page</taglib-uri>
<taglib-location>
/WEB-INF/lib/sitemesh-page.tld
</taglib-location>
</taglib>
<taglib>
<taglib-uri>sitemesh-decorator</taglib-uri>
<taglib-location>
/WEB-INF/lib/sitemesh-decorator.tld
</taglib-location>
</taglib>


在web/WEB-INF在定义两个基本的sitemesh配置文件,这两个文件可以sitemesh包复制过来。
sitemesh.xml定义最基本的应用规则。
<sitemesh>
<property name="decorators-file" value="/WEB-INF/decorators.xml"/>
<excludes file="${decorators-file}"/>
<page-parsers>
<parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
</page-parsers>
<decorator-mappers>
<mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
<param name="property.1" value="meta.decorator" />
<param name="property.2" value="decorator" />
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.FrameSetDecoratorMapper">
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.AgentDecoratorMapper">
<param name="match.MSIE" value="ie" />
<param name="match.Mozilla [" value="ns" />
<param name="match.Opera" value="opera" />
<param name="match.Lynx" value="lynx" />
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
<param name="decorator" value="printable" />
<param name="parameter.name" value="printable" />
<param name="parameter.value" value="true" />
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.RobotDecoratorMapper">
<param name="decorator" value="robot" />
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper">
<param name="decorator.parameter" value="decorator" />
<param name="parameter.name" value="confirm" />
<param name="parameter.value" value="true" />
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.FileDecoratorMapper">
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
<param name="config" value="${decorators-file}" />
</mapper>
</decorator-mappers>
</sitemesh>


decorators.xml中定义具体的decortor应用规则。
<?xml version="1.0" encoding="ISO-8859-1"?>

<decorators defaultdir="/decorators">
<!-- Any urls that are excluded will never be decorated by Sitemesh -->
<excludes>
<pattern>/exclude.jsp</pattern>
<pattern>/exclude/*</pattern>
</excludes>
<decorator name="main" page="main.jsp">
<pattern>/*</pattern>
</decorator>
<decorator name="panel" page="panel.jsp"/>
</decorators>


3.定义Decorator。
在Web Pages下新建一个目录,命名为decorators,在这个新建的目录中新建main.jsp,作为主要的decorator。
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %>
<html>
<head>
<title>Demo-<decorator:title default="Struts Sitemesh Demo..." /></title>
<!--link href="<%= request.getContextPath() %>/decorators/main.css" rel="stylesheet" type="text/css"-->
<decorator:head />
</head>
<body>
<div>
<table width="100%">
<tr>
<td id="pageTitle">
<h1> <decorator:title /></h1>
</td>
</tr>
<tr>
<td valign="top" height="100%">
<decorator:body />
</td>
</tr>
<tr>
<td id="footer">
<page:applyDecorator page="/footer.html" name="panel" />
</td>
</tr>
</table>
</div>
</body>
</html>


另外再定义一个panel。
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>

<p>
<table width="100%" border=0 cellpadding=0 cellspacing=0>
<tr>
<td class="panelBody">
<decorator:body />
</td>
</tr>
</table>
</p>


文件中,decorator:title和decorator:body定义要被替换的内容位置,应用之后,分别替换成decoratored页面的title和body标签中间的内容。
下面将Web Pages下的页面恢复成原貌。如index.jsp内容如下。
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<html>
<head><title>HomePage </title></head>
<body>
Welcome,
<logic:notPresent name="username" scope="session">
Guest !<html:link forward="newLogon">Logon </html:link> or
<html:link forward="newRegister">Register Now...</html:link>
</logic:notPresent>
<logic:present name="username" scope="session">
<bean:write name="username" scope="session"/>,
<html:link forward="logout">Log Out..</html:link>
</logic:present>
</body>
</html>


同时还要修改Struts定义文件,将上一节中tiles定义的page名称全部还要成具体的页面路径。
<action-mappings>
<action path="/logout" type="com.myapp.web.LogoutAction"/>
<action input="/register.jsp"
name="UserForm"
path="/register"
scope="session"
type="com.myapp.web.RegisterAction">
<forward name="success" path="/registerSuccess.jsp"/>
</action>
<action input="/logon.jsp"
name="UserForm"
path="/logon"
scope="session"
type="com.myapp.web.LogonAction"/>
<action path="/Welcome" forward="/welcomeStruts.jsp"/>
<action path="/index" forward="/index.jsp"/>
<action path="/newLogon" forward="/logon.jsp"/>
<action path="/newRegister" forward="/register.jsp"/>
</action-mappings>


另外还要禁用tiles,以免和sitemesh发生冲突。注释掉controller定义,让Struts使用默认的requestProcessor。
4.运行程序,测试效果。

[url]http://blog.chinaunix.net/u/1096/showart_476636.html[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值