选择显示字体大小

基于tiles框架struts框架的ui 设计—几种组织html和jsp视图组件

作者:        prakash malani
原文:        http://www.javaworld.com/javaworld/jw-01-2002/jw-0104-tilestrut.html
译者:        sunnyrainy
        email: sunnyrainy5@hotmail.com
        qq: 273771372
        msn: yo13579@sohu.com
摘要
每个web应用开发者一定组织视图组件,例如  页首,正文体和页尾。 有许多技术可以组织这些组件, 但每个都有优缺点。这篇文章包括了7个可用的解决方案并让你了解 tiles框架struts框架的灵活性。从一个简单的说明组织视图组件的例子开始,然后使用 jsp内建的机制,像include, 彻底地解决问题。继续看其他的使用tiles框架的替换方案,然后学会平衡tiles框架struts框架的协作。 (2,000个字;2002 年1月 4 日)
web应用软件开发中,一般用户界面负责站点的外观感觉,它可以让人产生真切的感受。通过界面的展现,来展示应用程序功能和导航。基于servletjsp技术实现用户界面时,当html页面被转换为servlets和jsps时,ui开发人员必须识别哪些是公共的普通html页面,哪些是jsp页面组件,如标题、页脚,主体,菜单和搜索等。这篇文章提供了多种有效的解决方案来组织htmljsp视图组件。每个方案都使用了特定的标准,像页数、编码重复和布局控制等。
为了探究模板和布局方案,我们使用tiles 框架.tiles 框架视图组件叫做tiles。框架 使用xml配置文件来组织这些tiles.框架不仅能让你再次使用tiles,而且还可以对他们进行设计组织。
为了探寻更强大和灵活的方案,我们研究tiles框架struts 框架之间的协作。struts是一个使用流行的mvc或者model 2结构模式的用于开发web 应用软件的开源框架.struts 有着强大标签库,tile框架的标签库加入使其更加完善。

评价标准
评价每个方案有如下标准。标准是兼容的。对于特定的情况和特殊的应用,必须经常均衡每个方案中这些因素的优势与缺陷。

页数(page number)
方案必须尽量使html and jsp的页数减到最小。因为页数增加,开发、管理、维护一个应用程序的复杂性将会急剧增加。

代码重复(code repetiton)
在大多数情况下,重复是不好的。html and jsp编码越是重复,开发和维护应用程序就越困难。一个简单变化可能在许多不同页面上导致一连串不可预期结果的改变。具体和实际的获得重新使用的方式应避免编码重复。

布局控制(layout comtrol)
尽管编码重复是不好的,但是布局逻辑和编码重复的会更糟。在几个jsps中的扩展视图组件结构的逻辑和行为可以是一个解决灾难的方法。获得模板的设计逻辑的重复使用是一种比仅仅重复使用视图组件更好的形式。因此,你可以通过有效的布局控制达到一个更高的重复使用的水平。

耦合(coupling)
耦合是个实体间的兼容性的一个度。软件工程师被重复教育要使不相干的类、包等等之间的耦合减到最小。我们可以对视图组件应用相同的原理。尽管从一个使用者角度讲视图组件之间是有区别的,但在jsp执行中,组建可能被杂乱的连接。一种解决方案使必须减少不相干视图组件之间的耦合.

复杂性(complexity)
复杂性导致了更多的开发和维护费用,也使得更多的复杂方案不合适。当你增加更多的部分时,复杂性同时也在增加,这将使原本看起来简单的和无害的东西很快转变为大混乱。

解决方案
我将使用基本的jsps例子。通过像header ,footer这样的普通的视图组件来评价一些方案。我将按增加复杂性的顺序给出这些方案,然后相对于评价标准详细的评价每一个。
方案1:基本的jsp
假如a.jsp如下:


<html>
<body>
header<p>
a's body...<p>
footer<p>
</body>
</html>


假如b.jsp如下:


<html>
<body>
header<p>
b's body...<p>
footer<p>
</body>
</html>


    在许多情况,开发者从ui类中直接获取编码,并根据需要直接将他转换成jsp代码。如上所示,每个jsp有重复的header 和footer。方案1是不合需求的,原因是header和 footer视图组件改变,因每页都为所放置的组件负责,所以需要在所有相关页上改变。简单的方案1缺乏远见。有着如此多htmljsp 代码重复,我们虽然减少了页的数量,但是需要一个很大的维护费用。就像前面所解释的,不同的视图组件间有着很大的耦合 ,所以这是不合需要的。

方案2:jsp include
假如 a.jsp如下:


<html>
<body>

<%-- include header --%>
<jsp:include page=&quot;/header.jsp&quot; />

a's body...
<p>

<%-- include footer --%>
<jsp:include page=&quot;/footer.jsp&quot; />

</body>
</html>


假如b.jsp如下:


html>
<body>

<%-- include header --%>
<jsp:include page=&quot;/header.jsp&quot; />

b's body...
<p>

<%-- include footer --%>
<jsp:include page=&quot;/footer.jsp&quot; />

</body>
</html>
注意到普通的诸如header和footer这样的视图组件,使用jsp 的include结构使其分开来了。
假如 header.jsp如下:
header
<p>
假如footer.jsp如下:
footer
<p>


    方案2很好的处理了方案1中的一些重要的缺陷。你仅需要改变一次通用的视图组件。因此,这个方案在很大程度上消除了htmljsp code重复,尤其是提高了应用程序的可维护性。他增加了一些页数,但他彻底的减少了普通视图组件和其他页之间的轻微的耦合.在复杂度上,这个方案可以很简单和容易的使用在现有应用程序中。但是,他有一个最主要的缺点:如果你改变了如何组织和哪里放置视图组件(例如,改变组件的布局),你必须更新每一页--导致了额外的、禁止的改变。方案2成功的使视图组件可以重复使用,但是不能重复使用布局和模板逻辑。
方案3 tiles insert


假如a.jsp如下:
<%@ taglib uri=&quot;/web-inf/tiles.tld&quot; prefix=&quot;tiles&quot; %>

<html>
<body>

<%-- include header --%>
<tiles:insert page=&quot;/header.jsp&quot; flush=&quot;true&quot;/>

a's body...
<p>

<%-- include footer --%>
<tiles:insert page=&quot;/footer.jsp&quot; flush=&quot;true&quot;/>

</body>
</html>

假如b.jsp如下:
<%@ taglib uri=&quot;/web-inf/tiles.tld&quot; prefix=&quot;tiles&quot; %>

<html>
<body>

<%-- include header --%>
<tiles:insert page=&quot;/header.jsp&quot; flush=&quot;true&quot;/>

b's body...
<p>

<%-- include footer --%>
<tiles:insert page=&quot;/footer.jsp&quot; flush=&quot;true&quot;/>

</body>
</html>

   方案3 使用tiles insert 机制来代替jsp include 结构。使用tiles insert 标签,你可将视图组件放置到恰当的位置。在其它所以方面,方案精确的镜像了jsp include方案(方案2),包括了优点和缺点。

方案4:spiltting bodies


假如 a.jsp如下:
<%@ taglib uri=&quot;/web-inf/tiles.tld&quot; prefix=&quot;tiles&quot; %>

<html>
<body>

<%-- include header --%>
<tiles:insert page=&quot;/header.jsp&quot; flush=&quot;true&quot;/>

<%-- include body --%>
<tiles:insert page=&quot;abody.jsp&quot; flush=&quot;true&quot;/>

<%-- include footer --%>
<tiles:insert page=&quot;/footer.jsp&quot; flush=&quot;true&quot;/>

</body>
</html>
假如b.jsp如下:
<%@ taglib uri=&quot;/web-inf/tiles.tld&quot; prefix=&quot;tiles&quot; %>

<html>
<body>

<%-- include header --%>
<tiles:insert page=&quot;/header.jsp&quot; flush=&quot;true&quot;/>

<%-- include body --%>
<tiles:insert page=&quot;bbody.jsp&quot; flush=&quot;true&quot;/>

<%-- include footer --%>
<tiles:insert page=&quot;/footer.jsp&quot; flush=&quot;true&quot;/>

</body>
</html>

方案4与tiles insert方案有些小的不同。方案4将核心主体分隔成几个单独页面,如abody.jsp and abody.jsp.
假如 abody.jsp如下:
a's body...
<p>
假如bbody.jsp如下:
b's body...
<p>

   方案4 的优点:他限制了body转变到各个页。同时,在其他地方他也让你能重复使用bodies,消除了重复的需要。因此,这个方案更加减少了通用视图组件于其他应用软件组件间的coupling 。产生和管理每个body组件引进了一个附加的复杂度。因为用其它的方案的话,每页依旧是他自己的布局。因此,没有过多的布局法则或模式

方案5:templating tiles
    使用tiles的框架特性,我们可将下面的布局作为模板定义(来自下面所示的layout.jsp文件)。因为这个布局,所以用tiles tag来插入标识符代替实际的视图组件。因此,对于所有组件而言,这页定义了一个可重复使用的布局:


<%@ taglib uri=&quot;/web-inf/tiles.tld&quot; prefix=&quot;tiles&quot; %>

<html>
<body>

    <%-- include header --%>
    <tiles:insert attribute=&quot;header&quot;/>
    
    <%-- include body --%>
    <tiles:insert attribute=&quot;body&quot;/>
    
    <%-- include footer --%>
    <tiles:insert attribute=&quot;footer&quot;/>

</body>
</html>
其它的内容页,像a.jsp and b.jsp使用如上布局来放置组件。在实际的页面里,使用tiles insert tag来插入布局。使用tiles put tag可以为布局中特定的标识符指定实际的视图组件。
假如a.jsp如下:
<%@ taglib uri=&quot;/web-inf/tiles.tld&quot; prefix=&quot;tiles&quot; %>

<tiles:insert page=&quot;/layout.jsp&quot; flush=&quot;true&quot;>
    <tiles:put name=&quot;header&quot; value=&quot;/header.jsp&quot;/>
    <tiles:put name=&quot;body&quot; value=&quot;/abody.jsp&quot;/>
    <tiles:put name=&quot;footer&quot; value=&quot;/footer.jsp&quot;/>    
</tiles:insert>
假如b.jsp如下:
<%@ taglib uri=&quot;/web-inf/tiles.tld&quot; prefix=&quot;tiles&quot; %>

<tiles:insert page=&quot;/layout.jsp&quot; flush=&quot;true&quot;>
    <tiles:put name=&quot;header&quot; value=&quot;/header.jsp&quot;/>
    <tiles:put name=&quot;body&quot; value=&quot;/bbody.jsp&quot;/>
    <tiles:put name=&quot;footer&quot; value=&quot;/footer.jsp&quot;/>    
</tiles:insert>

   方案5最突出的优势是他封装了布局原理图或结构方式,彻底的减少了通用视图组件和其他内容主体间的coupling。然而,他引入了其他布局页面增加了复杂性。首先,理解与执行模块就有困难。
方案6:struts and tiles
上面的layout.jsp布局页面,包含了组织组件的html and jsp code.内容页a.jsp and b.jsp不包含任何的html code;仅仅包括了tiles tags来插入必需的组件。在一个xml文件中指定所有的内容页面会好一点吗?让我们来tiledefinitions.xml文件,指定其页面如下:


<?xml version=&quot;1.0&quot; encoding=&quot;iso-8859-1&quot;?>
<component-definitions>
    <definition name=&quot;adef&quot; path=&quot;/layout.jsp&quot;>
        <put name=&quot;header&quot; value=&quot;/header.jsp&quot;/>
        <put name=&quot;footer&quot; value=&quot;/footer.jsp&quot;/>
        <put name=&quot;body&quot; value=&quot;/abody.jsp&quot;/>
    </definition>
    <definition name=&quot;bdef&quot; path=&quot;/layout.jsp&quot;>
        <put name=&quot;header&quot; value=&quot;/header.jsp&quot;/>
        <put name=&quot;footer&quot; value=&quot;/footer.jsp&quot;/>
        <put name=&quot;body&quot; value=&quot;/bbody.jsp&quot;/>
    </definition>
    <definition name=&quot;cdef&quot; path=&quot;/layout.jsp&quot;>
        <put name=&quot;header&quot; value=&quot;/header.jsp&quot;/>
        <put name=&quot;footer&quot; value=&quot;/footer.jsp&quot;/>
        <put name=&quot;body&quot; value=&quot;/cbody.jsp&quot;/>
    </definition>
</component-definitions>
方案6把定义放置在xml文件中,消除了就像a.jsp and b.jsp一样的所有内容页。因像a.jsp and b.jsp这样的程序不再存在,我们怎样请求他?更重要的是,如何在tiledefinitions.xml文件中请求定义?
struts and tiles强大和协作的整体可被挽回。除了常规的配置参数以外,我们在web.xml文件中作为另一个参数指定配置文件的地址。如下所示。指定参数definitions_config使struts能找到并知道关于tiles 的定义。
<!-- standard action servlet configuration (with debugging) -->
  <servlet>
    <servlet-name>action</servlet-name>
<!--
    <servlet-class>org.apache.struts.action.actionservlet</servlet-class>
-->
    <servlet-class>org.apache.struts.tiles.actioncomponentservlet</servlet-class>
    <init-param>
        <param-name>definitions-config</param-name>
        <param-value>/web-inf/tiledefinitions.xml</param-value>
    </init-param>
    ...
  </servlet>
我们现在定义一个struts action,他将返回一个成功的在配置文件中指定的定义。struts action dofirst 是一个不运行的行为,如下所示:
package com.malani.struts.action;

import org.apache.struts.action.*;
import javax.servlet.http.*;

public class dofirst extends action {

    public actionforward perform(
        actionmapping amapping,
        actionform aform,
        httpservletrequest arequest,
        httpservletresponse aresponse
    ) {
        return amapping.findforward(&quot;success&quot;);
    }

}

你不能使用直接来自浏览器的定义,但是 你可以使用来自struts的就像实际程序一样的一个。在struts-config.xml的文件中定义struts actions如下:
<action path=&quot;/a&quot;
            type=&quot;com.malani.struts.action.dofirst&quot;
    >
        <forward name=&quot;success&quot; path=&quot;adef&quot;/>
    </action>

    <action path=&quot;/b&quot;
            type=&quot;com.malani.struts.action.dofirst&quot;
    >
        <forward name=&quot;success&quot; path=&quot;bdef&quot;/>
    </action>

    <action path=&quot;/c&quot;
            type=&quot;com.malani.struts.action.dofirst&quot;
    >
        <forward name=&quot;success&quot; path=&quot;cdef&quot;/>
    </action>
现在通过分别请求a.do and b.do actions来返回所需的程序。

   方案6的主要优点是他把所有的定义都统一到一个xml配置文件里。减少内容页很彻底的减少了总的页数。通过引进struts,在另个方法上增加了复杂性。
方案7:tiles inheritance
      在定义配置文件中,可观察到每页定义看起来是一样的。每个定义有三个组件,两个被固定为header and footer.强大的tiles特性使得定义间有了继承性。因此,你可以定义一个基础定义,让原始定义从那个定义上继承来。原始的定义必须仅支持他们的单一部分。下面显示了带有定义间继承性的xml配置文件:


<?xml version=&quot;1.0&quot; encoding=&quot;iso-8859-1&quot;?>
<component-definitions>
    <definition name=&quot;basedef&quot; path=&quot;/layout.jsp&quot;>
        <put name=&quot;header&quot; value=&quot;/header.jsp&quot;/>
        <put name=&quot;footer&quot; value=&quot;/footer.jsp&quot;/>
        <put name=&quot;body&quot; value=&quot;&quot;/>
    </definition>
    <definition name=&quot;adef&quot; extends=&quot;basedef&quot;>
        <put name=&quot;body&quot; value=&quot;/abody.jsp&quot;/>
    </definition>
    <definition name=&quot;bdef&quot; extends=&quot;basedef&quot;>
        <put name=&quot;body&quot; value=&quot;/bbody.jsp&quot;/>
    </definition>
    <definition name=&quot;cdef&quot; extends=&quot;basedef&quot;>
        <put name=&quot;body&quot; value=&quot;/cbody.jsp&quot;/>
    </definition>
</component-definitions>

     这个方案的优势是消除了配置文件理重复和多余的信息。总之,这个方案的优点和缺点与struts and tiles方案的一样。

方案总结
     如下表格总结了各个方案的评价指标方面的内容。我鼓励你增加其他的有创造性的方案,和其他重要的,像可延展性、可维护性、可操作性这样的评价指标。
各种解决方案的每项评估指标:


     表格表明每个方案的复杂度是逐渐上升的。同时他也表明当你增加复杂度时,减少了编码复制,增加了设计控制的灵活性,减少了不相干视图组件的coupling.页数最初是当视图组件分开时增加的,但当你在一个定义结构之中定义了更多页的时候,合并会发生。
那个方案是最好的?
最好的方案是要依据你项目所需和你在开发和维护一个web应用程序中的技能和知识。基本的方案太简单;我并不推荐他,因为他不支持好的软件工程师的实践经验的思路。如果你的web应用程序是复杂的,模板给你提供了一个很好的布局控制。因此你可能想研究和使用一个像tiles一样的模板框架。如果你已经使用了struts,接着你必须为一个强大的方案调节tiles and struts之间的协作。
divine design
在这篇文章里,评价了不同的在html and jsps中组织视图组件的方案。同时也研究了struts and tiles框架之间的协作性。这些策略和方案可以帮助你做一个有关于你的web应用程序的设计和结构的决定。
对审阅这篇文章的max cooper, stephen ditlinger, dru jensen, phillip lindsay, roshni malani, danny trieu, and clare zhang致以最诚挚的谢意。
资源:
方案1-7源代码:
http://www.javaworld.com/javaworld/jw-01-2002/tilestrut/1_basic.zip
http://www.javaworld.com/javaworld/jw-01-2002/tilestrut/2_jspinclude.zip
http://www.javaworld.com/javaworld/jw-01-2002/tilestrut/3_tilesinsert.zip
http://www.javaworld.com/javaworld/jw-01-2002/tilestrut/4_bodies.zip
http://www.javaworld.com/javaworld/jw-01-2002/tilestrut/5_templatingtiles.zip
http://www.javaworld.com/javaworld/jw-01-2002/tilestrut/6_struts.zip
http://www.javaworld.com/javaworld/jw-01-2002/tilestrut/7_inheritance.zip
tiles框架
http://www.lifl.fr/~dumoulin/tiles/


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons