`

Tiles的使用方法

    博客分类:
  • Java
阅读更多
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>


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

----------------------------------------------------------------------------------
另外一种布局方法是用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.运行程序,测试效果。

http://blog.chinaunix.net/u/1096/showart_476636.html
分享到:
评论

相关推荐

    tiles2.1.2前端框架

    2. `tiles-jsp-2.1.2.jar`:这个JSP标签库文件使开发者能够在JSP页面中方便地使用Tiles。通过这些标签,你可以轻松地插入、组合和管理不同的页面部分。 3. `tiles-servlet-2.1.2.jar`:这个组件是与Servlet容器交互...

    使用Tiles 中文

    ### 使用Tiles进行Web应用布局管理 #### 知识点一:Tiles框架的引入与优势 在Web开发领域,特别是基于Java的Struts框架项目中,布局管理与内容组织一直是复杂而重要的任务。传统的做法,如直接在JSP页面中混杂表示...

    SpringMVC使用tiles

    下面我们将详细讨论如何在SpringMVC项目中使用Tiles。 1. **Tiles简介** Tiles框架的核心概念是“定义-使用”模式。定义是指创建一个页面模板,其中包含可替换的部分(称为“部件”),然后在实际页面中使用这些...

    struts-tiles框架

    4. **在Action中使用Tiles**:在Action中,通过调用特定的方法,如`ActionForward`,来指定返回哪个Tiles定义,从而决定显示哪个页面。 5. **在JSP中使用Tiles**:在视图层,JSP页面可以包含Tiles,使用`&lt;tiles:...

    Struts2 tiles操作例子

    在探讨“Struts2 tiles操作例子”这一主题时,我们首先需要理解Struts2框架以及tiles插件在其中扮演的角色...对于任何想要提升Web应用开发效率的开发者来说,掌握Struts2 tiles插件的使用方法都是必不可少的技能之一。

    Struts1-tiles标签

    创建复合式网页的方法多种多样,例如使用HTML框架标签,但这通常是静态的;通过Dreamweaver等页面设计工具创建的页面模板也会导致代码冗余;而采用JSP的`&lt;jsp:include&gt;`动作或指令则能动态地将共享内容包含到各个JSP...

    struts2 tiles 模板搭建源码可运行

    这个压缩包文件包含了使用Struts2和Tiles框架搭建的模板源码,便于学习和参考。 在Struts2中,Tiles框架的主要功能包括: 1. **页面模板**:Tiles允许定义页面模板,这些模板包含可重用的页面片段,如头部、底部和...

    Struts2+Tiles

    通过阅读和学习《Struts2+Tiles》相关的博客文章(如博文链接所示),你可以深入理解这两者的集成方式,如何配置Struts2的struts.xml和tiles.xml文件,以及如何在Action中使用Tiles API来决定页面内容。此外,你还能...

    一个简单的应用Struts中tiles的例子

    4. **使用Tiles动作**:在Action类中,通过调用`TilesUtil`的`putAttribute`方法将数据放入作用域,然后重定向到Tiles定义的名称。例如: ```java public ActionForward execute(ActionMapping mapping, ...

    springmvc与apache tiles整合demo

    然而,当项目需要更复杂的页面结构和布局时,如多个部分需要重复使用,或者需要灵活控制页面模板,这时Apache Tiles就派上用场了。 Apache Tiles的核心概念是"Tiles",也就是瓷砖,这些瓷砖可以是HTML片段、JSP页面...

    springboot- thymeleaf-tiles-demo

    5. **编写控制器**:在SpringBoot的Controller类中,定义处理请求的方法,并返回tiles的名称。SpringBoot会自动根据tiles配置将这些名称映射到对应的模板。 6. **运行和测试**:启动SpringBoot应用,通过浏览器访问...

    jsf入门列子和jsf与Tiles结合

    具体集成步骤包括:引入Tiles的依赖库,配置`web.xml`以启用Tiles,编写Tiles的定义文件(如`tiles-defs.xml`),然后在JSF页面中引用Tiles组件。通过这种方式,开发者可以创建可复用的页面片段,并在多个JSF视图...

    tiles框架增加一个页面的步骤

    ### Tiles框架增加一个页面的步骤详解 #### 一、引言 在Web开发中,保持一致的页面布局和样式对于提升用户体验至关重要。...总之,熟练掌握Tiles框架的使用方法,对于提升Web开发效率有着重要意义。

    springmvc 集成 tiles2实现页面模板局部刷新

    4. **局部刷新**:在Tiles2中,实现局部刷新的关键在于正确地组织页面结构和使用AJAX。每个可刷新的区域可以视为一个单独的tile,通过AJAX请求更新该区域的内容。使用JavaScript库如jQuery或者Vue.js,可以方便地...

    tiles 官方教程

    在开始使用Tiles之前,需要配置Web应用程序。这通常涉及到在`web.xml`文件中添加Tiles的监听器和初始化参数,以及配置Tiles的定义文件,如`tiles-defs.xml`。这些定义文件包含了页面组件的布局信息。 **基本使用...

    apache tiles3.0

    在本篇文章中,我们将深入探讨Apache Tiles 3.0的核心特性、使用方法以及如何在实际开发中应用。 一、Apache Tiles 3.0简介 Apache Tiles的主要目标是通过定义和重用页面模板来简化Web应用的视图层开发。Tiles 3.0...

    Tiles框架包

    然后,在控制器方法中返回一个表示页面布局的定义名称,Tiles会根据这个名称自动组合各个tiles来生成最终的HTML页面。 总的来说,Tiles框架包结合Spring框架,为Web应用开发提供了强大的视图管理能力,使得页面布局...

    Tiles布局

    4. **在Spring MVC控制器中使用**:在控制器方法中,返回一个表示Tiles布局的视图名,而不是具体的JSP页面名。 5. **编写瓦片页面**:每个瓦片对应一个单独的JSP页面,它们会被组合到一起形成最终的用户界面。 6. ...

    spring mvc, tiles, freemarker集成

    6. **编写控制器**:使用Spring MVC的@Controller和@RequestMapping注解编写处理HTTP请求的控制器方法,返回一个定义了Tiles视图名称的字符串。 7. **创建模板**:在Freemarker模板文件中,编写HTML代码,使用...

    Python库 | gdal2tiles-0.1.8.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:gdal2tiles-0.1.8.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

Global site tag (gtag.js) - Google Analytics