sitemesh_文档集锦_1

来源:互联网 发布:淮安淘宝运营培训班 编辑:程序博客网 时间:2024/06/03 20:17

使用sitemesh

 

使用sitemesh建立复合视图 - 1.hello. 1

使用sitemesh建立复合视图 - 2.装饰器  decorator 4

sitemesh.xml 可以设置2种信息: 5

使用sitemesh建立复合视图- 3.其他讨论... 9

记事贴:我被AppFuseSiteMesh折磨了2天!... 12

sitemesh简化布局... 14

SiteMesh 优雅地分离页面的内容与框架... 18

AppFuse里消息显示机制... 19

 

 

使用sitemesh建立复合视图 - 1.hello. 1

使用sitemesh建立复合视图 - 2.装饰器  decorator 4

sitemesh.xml 可以设置2种信息: 5

使用sitemesh建立复合视图- 3.其他讨论... 9

记事贴:我被AppFuseSiteMesh折磨了2天!... 12

sitemesh简化布局... 14

SiteMesh 优雅地分离页面的内容与框架... 18

AppFuse里消息显示机制... 19

 


word文档下载:http://www.chq.name/component/option,com_docman/task,doc_download/gid,10/

使用sitemesh建立复合视图 - 1.hello

使用sitemesh建立复合视图 - 1.hello

(作者:chen-neu ,提供给 huihoo.com 发布)


使用sitemesh建立复合视图 - 1.hello  <- now

使用sitemesh建立复合视图 - 2.装饰器  

使用sitemesh建立复合视图 - 3.其它讨论 

sitemesh是opensymphony团队开发的j2ee应用框架之一,旨在提高页面的可维护性和复用性。opensymphony的另一个广为人知的框架为webwork是用作web层的表示框架。他们都是开源的,可以在www.sf.net下找到。

应用于以下大项目的例子:http://opensource.thoughtworks.com/projects/sitemesh.html

简介:

sitemesh应用Decorator模式,用filter截取requestresponse,把页面组件head,content,banner结合为一个完整的视图。通常我们都是用include标签在每个jsp页面中来不断的包含各种header, stylesheet, scripts and footer,现在,在sitemesh的帮助下,我们可以开心的删掉他们了。如下图,你想轻松的达到复合视图模式,那末看完本文吧。

 

hello sitemesh

  1. WEB-INF/web.xmlcopy以下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>
 
<taglib>
  <taglib-uri>sitemesh-decorator</taglib-uri>
  <taglib-location>/WEB-INF/sitemesh-decorator.tld</taglib-location>
</taglib>
 
<taglib>
  <taglib-uri>sitemesh-page</taglib-uri>
  <taglib-location>/WEB-INF/sitemesh-page.tld</taglib-location>
</taglib>

 

  1. copy所需jardtd文件至相应目录,访问opensymphony.sourceforge.netcvs以获取sitemesh最新版本。

sitemesh.jar

WEB-INF/lib

sitemesh-decorator.tld

WEB-INF

sitemesh-page.tld

WEB-INF

  1.  
  2. 建立WEB-INF/decorators.xml描述各装饰器页面(可仿照sitemesh例子)
<decorators defaultdir="/_decorators">
    <decorator name="main" page="main.jsp">
        <pattern>*</pattern>
    </decorator>
</decorators>

5.     

  1. 建立装饰器页面 /_decorators/main.jsp
<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="sitemesh-decorator" prefix="decorator" %>
 
<html>
  <head>
    <title><decorator:title default="装饰器页面..." /></title>
    <decorator:head />
  </head>
  <body>
    sitemesh的例子<hr>
    <decorator:body />
    <hr>chen56@msn.com
  </body>
</html>

 

  1. 建立一个的被装饰页面 /index.jsp(内容页面)
<%@ page contentType="text/html; charset=GBK"%>
<html>
  <head>
    <title>Agent Test</title>
  </head>
  <body>
    <p>本页只有一句,就是本句.</p>
  </body>
</html>

最后访问index.jsp,将生成如下页面:

而且,所有的页面也会如同index.jsp一样,被sitemesh的filter使用装饰模式修改成如上图般模样,却不用再使用include标签。

 

使用sitemesh建立复合视图 - 2.装饰器  decorator

使用sitemesh建立复合视图 - 2.装饰器  decorator


使用sitemesh建立复合视图 - 1.hello 

使用sitemesh建立复合视图 - 2.装饰器  <- now

使用sitemesh建立复合视图 - 3.其它讨论

装饰器  decorator概念

建立可复用的web应用程序,一个通用的方法是建立一个分层系统,如同下面一个普通的web应用:

  • 前端,front-end:JSPServlets,或jakartavelocity
  • 控制层框架 Controller (Struts/Webwork)
  • 业务逻辑 Business :主要业务逻辑

·         持久化框架 :hibernate/jdo

可糟糕的是前端的页面逻辑很难被复用,当你在每一个页面中用数之不尽的include来复用公共的header, stylesheet, scripts,footer时,一个问题出现了-重复的代码,每个页面必须用copy来复用页面结构,而当你需要创意性的改变页面结构时,灾难就爱上了你。

sitemesh通过filter截取request和response,并给原始的页面加入一定的装饰(可能为header,footer...),然后把结果返回给客户端,并且被装饰的原始页面并不知道sitemesh的装饰,这也就达到了脱耦的目的。

据说即将新出台的Portlet规范会帮助我们标准的实现比这些更多更cool的想法,但可怜的我还不懂它到底是一个什末东东,有兴趣的人可以研究
jetspeed,或JSR (Java Specification Request) 168,但我想sitemesh如此简单,我们不妨先用着。

 

让我们看看怎样配置环境

除了要copyWEB-INF/lib中的sitemesh.jar, copyWEB-INF中的sitemesh-decorator.tld,sitemesh-page.tld文件外,还有2个文件要建立到WEB-INF/

  • sitemesh.xml (可选)  
  • decorators.xml 

sitemesh.xml 可以设置2种信息:

Page Parsers :负责读取stream的数据到一个Page对象中以被SiteMesh解析和操作。(不太常用,默认即可)

Decorator Mappers : 不同的装饰器种类,我发现2种比较有用都列在下面。一种通用的mapper,可以指定装饰器的配置文件名,另一种可打印的装饰器,可以允许你当用http://localhost/aaa/a.html?printable=true方式访问时给出原始页面以供打印(免得把header,footer等的花哨的图片也搭上)

(但一般不用建立它,默认设置足够了:com/opensymphony/module/sitemesh/factory/sitemesh-default.xml):

范例:

DE><sitemesh>DE>
DE>  DE>DE><page-parsers>DE>
DE>    DE>DE><parser default="true" class="com.opensymphony.module.sitemesh.parser.DefaultPageParser" />DE>
DE>    DE>DE><parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.FastPageParser" />DE>
DE>    DE>DE><parser content-type="text/html;charset=ISO-8859-1" class="com.opensymphony.module.sitemesh.parser.FastPageParser" />DE>
DE>  DE>DE></page-parsers>DE>

DE>  DE>DE><decorator-mappers>DE>
DE>    DE>DE><mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">DE>
DE>      DE>DE><param name="config" value="/WEB-INF/decorators.xml" />DE>
DE>    DE>DE></mapper>DE>
      <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>

DE>  DE>DE></decorator-mappers>DE>
DE></sitemesh> DE>

decorators.xml :定义构成复合视图的所有页面构件的描述(主要结构页面,header,footer...),如下例:

<decorators defaultdir="/_decorators">
  <decorator name="main" page="main.jsp">
    <pattern>*</pattern>
  </decorator>
  <decorator name="printable" page="printable.jsp" role="customer" webapp="aaa" />
</decorators>

 

  • defaultdir: 包含装饰器页面的目录
  • page : 页面文件名
  • name : 别名
  • role : 角色,用于安全
  • webapp : 可以另外指定此文件存放目录
  • Patterns : 匹配的路径,可以用*,那些被访问的页面需要被装饰。

 

最重要的是写出装饰器本身(也就是那些要复用页面,和结构页面)

其实,重要的工作就是制作装饰器页面本身(也就是包含结构和规则的页面),然后把他们描述到decorators.xml中。

让我们来先看一看最简单的用法:其实最常用也最简单的用法就是我们的hello例子,面对如此众多的技术,我想只要达到功能点到为止即可,没必要去研究太深(除非您有更深的需求)。

<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="sitemesh-decorator" prefix="decorator" %>
 
<html>
  <head>
    <title><decorator:title default="装饰器页面..." /></title>
    <decorator:head />
  </head>
  <body>
    sitemesh的例子<hr>
    <decorator:body />
    <hr>chen56@msn.com
  </body>
</html>

我们在装饰器页面只用了2个标签:

<decorator:title default="装饰器页面..." />    : 把请求的原始页面的title内容插入到<title></title>中间。

<decorator:body /> : 把请求的原始页面的body内的全部内容插入到相应位置。

然后我们在decorator.xml中加入以下描述即可:

<decorator name="main" page="main.jsp">
    <pattern>*</pattern>
</decorator>

这样,请求的所有页面都会被重新处理,并按照main.jsp的格式重新展现在你面前。

 

让我们看看更多的用法。(抄袭sitemesh文档)

以下列着全部标签:

 

<decorator:head />

插入原始页面(被包装页面)的head标签中的内容(不包括head标签本身)。

<decorator:body />

插入原始页面(被包装页面)的body标签中的内容。

<decorator:title [ default="..." ] />

插入原始页面(被包装页面)的title标签中的内容,还可以添加一个缺省值。

例:

/_decorator/main.jsp中 (装饰器页面): <title><decorator:title default="却省title-hello"  /> - 附加标题</title>

/aaa.jsp中 (原始页面):<title>aaa页面</title>

访问/aaa.jsp的结果:<title>aaa页面 - 附加标题</title>

<decorator:getProperty property="..." [ default="..." ] [ writeEntireProperty="..." ]/>

在标签处插入原始页面(被包装页面)的原有的标签的属性中的内容,还可以添加一个缺省值。

sitemesh文档中的例子很好理解:
The decorator: DE><body bgcolor="white"<decorator:getProperty property="body.onload" writeEntireProperty="true" />>DE>
The undecorated page: DE><body onload="document.someform.somefield.focus();">DE>
The decorated page: DE><body bgcolor="white" onload="document.someform.somefield.focus();">DE>

注意,DE>writeEntireProperty="true"会在插入内容前加入一个空格。DE>

<decorator:usePage id="..." />
象jsp页面中的<jsp:useBean>标签一样,可以使用被包装为一个Page对象的页面。 (懒的用)

例:可用<decorator:usePage id="page" /><%=DE>page.getTitle()%>达到<decorator:title/>的访问结果。DE>

 

<page:applyDecorator name="..." [ page="..." title="..." ] >
<page:param name="..."> ... </page:param>

<page:param name="..."> ... </page:param>
</page:applyDecorator>

应用包装器到指定的页面上,一般用于被包装页面中主动应用包装器。这个标签有点不好理解,我们来看一个例子:

包装器页面 /_decorators/panel.jsp:<p><decorator:title /></p>  ... <p><decorator:body /></p>
  并且在decorators.xml中有<decorator name="panel" page="panel.jsp"/>

一个公共页面,即将被panel包装:/_public/date.jsp:  
  ... <%=new java.util.Date()%>  ...<decorator:getProperty property="myEmail" />

被包装页面 /page.jsp : 
  <title>page的应用</title> 
  .....  
  <page:applyDecorator name="panel" page="/_public/date.jsp" >
    <page:param name="myEmail"> chen_p@neusoft.com </page:param>
  </page:applyDecorator>

最后会是什末结果呢?除了/page.jsp会被默认的包装页面包装上header,footer外,page.jsp页面中还内嵌了date.jsp页面,并且此date.jsp页面还会被panel.jsp包装为一个title加body的有2段的页面,第1段是date.jsp的title,第2段是date.jsp的body内容。

另外,page:applyDecorator中包含的page:param标签所声明的属性值还可以在包装页面中用decorator:getProperty标签访问到。


 

 使用sitemesh建立复合视图- 3.其他讨论

使用sitemesh建立复合视图 - 3.其他讨论


使用sitemesh建立复合视图 - 1.hello 

使用sitemesh建立复合视图 - 2.装饰器  

使用sitemesh建立复合视图 - 3.其它讨论 <- now

前面的文章已经足以应用sitemesh来改善您的应用,但我发现还有一些其他的东东可能也会对大家有所帮助

可打印的界面装饰

前面说过有1种可打印的装饰器,可以允许你当用http://localhost/aaa/a.html?printable=true方式访问时,应用其他的装饰器(自己指定),给出原始页面以供打印(免得把header,footer等的花哨的图片也搭上)

让我们来看一看怎样实现他:

1.首先在WEB-INFO/sitemesh.xml中设置:
  <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>
这样就可以通过?printable=true来使用名为printable的装饰器,而不是用原来的装饰器。

2.在WEB-INFO/decorators.xml中定义相应的printable装饰器
  <decorator name="printable" page="printable.jsp"/>

3.最后编写printable装饰器/decorators/printable.jsp

<%@ taglib uri="sitemesh-decorator" prefix="decorator" %>
<html>
<head>
  <title><decorator:title /></title>
  <decorator:head />
</head>
<body>

  <h1><decorator:title /></h1>
  <p align="right"><i>(printable version)</i></p>

  <decorator:body />

</body>
</html>

这样就可以让一个原始页面通过?printable=true开关来切换不同的装饰器页面。

 

 

中文问题

由于sitemesh内部所使用的缺省字符集为iso-8859-1,直接使用会产生乱码,我们可以通过以下方法纠正之:

  • 方法1:可以在您所用的application server的配置文件中找一找,有没有设置encodingDE>charset的项目,然后设成gbk或gb2312即可DE>
  • DE>方法2:这也是我们一直使用的方法。DE>
    DE>1.在每一个jsp页里设置: <%@ page contentType="text/html; charset=gbk"%> 来告诉server你所要求的字符集。DE>
    DE>2.在每个jsp页的head中定义:<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=gbk"> 来告诉浏览器你所用的字符集。DE>

 

总结:使用sitemesh最通常的途径:

1.配置好环境,

2.在WEB-INFO/decroators.xml中描述你将建立的包装器。

3.开发在decroators.xml中描述的包装器,最好存放在/_decorators目录下

4.ok ,可以看看辛勤的成果了 :)


资源:

关于作者:

陈鹏,西安东软公司。作为一名狂热的程序员希望每一天都能成长进步,并希望与大家分享快乐和知识。
请用以下方式和他联系:email chen56@msn.com  

 

 

记事贴:我被AppFuseSiteMesh折磨了2天!

兔八哥 (http://tb.blog.csdn.net/TrackBack.aspx?PostId=248919)

 

因为AppFuse中使用的默认的JSP模板为SiteMesh,默认情况下,所有的请求都会被SiteMesh所装饰,所以我要实现点击文件链接,直接下载文件时却得不到正确格式的文件,得到的是一个html文件,即使制定了下载文件的类型:如:application/msexcel或者其他的类型都不行,我分析是因为,我首先向输出流中写了些东西,后来被SiteMesh使用getWriter方法给冲掉了,所以得到的是默认的网页文件,我折腾了2天,痛不欲生,刚刚解决掉这个问题,解决方法如下:

 

1.将下载文件的jsp放到一个特殊的目录下,我的放在download目录,这样可以区分需要被sitemesh装饰和不需要被它装饰的文件。

 

2.配置sitemeshdecorators.xml:

<decorators defaultdir="/decorators">

    <excludes>

        <pattern>/download/*</pattern>

    </excludes>

</decorators>

 

这个配置指明download下的文件不需要被sitemesh装饰。

 

 

 

我的下载文件的页面代码是试验代码,稍加修改就可以正式使用,代码如下:

<%@ page import= "java.io.*,java.net.*" %><%

    try{

        //取得虚拟的路径

        String fn = "attachment; filename=a.xls";  //必须改为UniCode编码的字符串

        System.out.println(fn);

        //把标题、内容写到输出流中

        response.setHeader("Content-Disposition", new String(fn.getBytes("GB2312"),

                                        "ISO-8859-1"));

        createOutput( response.getOutputStream(),"c:\ltf.xls");

    }catch( Exception ee ){

        ee.printStackTrace();

    }

%><%!

public void createOutput( OutputStream out,String realpath ) throws IOException {

    int b;

    BufferedInputStream  m_input =

                new BufferedInputStream( new  FileInputStream(realpath) );

    while( (b = m_input.read()) != -1 ){

        out.write(b);

    }

    m_input.close();

    out.flush();

    out.close();

}

%>

<%--

  // 得到文件名字和路径

  String filename = request.getParameter("zipfilename");

  //String jsppath = "";             getServletConfig().getServletContext().getRealPath("") +

   String filepath = "c:\ltf.xls";        //jsppath.substring(0,jsppath.lastIndexOf("")) + "reports\";

  System.out.println("---------------=================" + filepath);

  java.io.FileInputStream fileInputStream = null;

  //out.println(filepath);

  // 设置响应头和下载保存的文件名

  response.setHeader("Content-Disposition",

  "attachment; filename=" + "a.xls");

  response.setContentType("application/msexcel");

 

  // 打开指定文件的流信息

  try{

  fileInputStream =

   new java.io.FileInputStream(filepath);

 

  // 写出流信息

  int i;

  while ((i=fileInputStream.read()) != -1) {

   out.write(i);

  }

  fileInputStream.close();

  out.close();

  System.out.println("---OK---------------");

 

  }catch(Exception ee){

    ee.printStackTrace();

  }

--%>

 

 

 

 

 

sitemesh简化布局

sitemesh简化布局

sitemesh一个系统,添加到Web应用中并促进模式HTML世界里的应用。
可从http://www.opensymphony.com/sitemesh/ 获得。

sitemesh
仅处理html的内容,象图象,PDF文件和下载这样的媒体是被忽略的。

下面我们将用实例来介绍如何分析内容网页和装饰器如何被映射到网页。以及从sitemesh中获取
信息的一些技术。

整个事例用到login.jsp, date.jsp , 索引的index.html
配置文件decorators.xml
装饰文件:window.jsp ,simple.jsp
运用的样式表:style.css


目标是把内容与布局分离,一达到简化布局,以及重用代码的作用。


实质是使用simple.jsp,window.jsp以及提取的longin.jsp,date,jsp的实际主体来构成最终显示
给用户的页面。


布局主体是simple.jsp控制:它控制了网页的整体架构。

<%@ taglib uri="sitemesh-decorator" prefix="decorator" %>
<%@ taglib uri=
"sitemesh-page" prefix="page" %>

<html>
<head>
<title><decorator:title/></title>
<link rel=
"stylesheet" href="decorators/style.css">
<decorator:head/>
</head>
<body>

<table width=
"100%">
<tr>
<td
class="title" colspan="2">
<decorator:title/>
</td>
</tr>
<tr>
<td
class="body" valign="top">
<decorator:body/>
</td>
<td valign=
"top">

<page:applyDecorator name=
"window" page="date.jsp"/>

<page:applyDecorator name=
"window" title="Disclaimer">
This site is not legally binding in any way. <br>
All rights reserved. Elvis has left the building.
</page:applyDecorator>

</td>
</tr>
<tr>
<td
class="footer" valign="top" colspan="2">
<b>Title:</b> <decorator:title/> <br>
<b>Author:</b> <decorator:getProperty property=
"meta.author"/> <br>
</td>
</tr>
</table>

</body>
</html>
这个文件将在decorators.xml中被定义,其中给出装饰器的名称,位置,模式匹配。
模式匹配可以使用通配符和正则表达式。在这对于simple.jsp使用了通配符*配置该
装饰器用来匹配Web应用中的所有网页。有关正则表达式的相关内容请参看本博客 http://192.168.0.3/blog3/meiking_archive_2005_05_20_18525.html)

<decorators>

<decorator name=
"simple" page="/decorators/simple.jsp">
<pattern>*</pattern>
</decorator>


</decorators>

上面我们讨论了装饰器设计模式,将装饰器和内容组合,下面我们来看看如何运用组合设计模式

在页面中引用子装饰器 ,子内容。
让我们来看看一个子装饰器window.jsp


<%@ taglib uri=
"sitemesh-decorator" prefix="decorator" %>
<table
class="window">
<tr>
<th><img src=
"decorators/snazzy.gif"><decorator:title/></th>
</tr>
<tr>
<td>
<decorator:body/>
</td>
</tr>
</table>

在回头看看simple.jsp在其中我们已经包含了window.jsp的运用,我们把他运用到了date.jsp
上,运用样式表date.jsp的主体内容应该会出现在最终显示页面的右上角。

decorators.xml中被定义
<decorators>

<decorator name=
"window" page="/decorators/window.jsp"/>

</decorators>



现在我们可以给出完整的decorators.xml了:
<decorators>

<decorator name=
"simple" page="/decorators/simple.jsp">
<pattern>*</pattern>
</decorator>

<decorator name=
"window" page="/decorators/window.jsp"/>

</decorators>
在装饰器中拥有一个技术:sitemesh有一个pageparser对象,它通过内容网页获取输出的内容
并把它解析到一个page对象中
假如一个内容网页有如下列标题:
<html>
<head>
<title>Please login</title>
<meta name=
"author" content="Homer Simpson">
</head>
...
装饰器使用JSP标识符可以访问title属性,也能访问auther属性
让我们看看simple是如何做的:
<%@ taglib uri=
"sitemesh-decorator" prefix="decorator" %>
...
<b>Title:</b> <decorator:title/> <br>
<b>Author:</b> <decorator:getProperty property=
"meta.author"/> <br>
...


简单的login.jsp,date.jsp页面

login.jsp

<html>
<head>
<title>Please login</title>
<meta name=
"author" content="Homer Simpson">
</head>
<body>
<form action=
"#" method="post">

<input type=
"hidden" name="section" value="store">

Login Name:<br>
<input type=
"text" name="loginname"><br>

Password:<br>
<input type=
"password" name="password"><br>

<input type=
"submit" value="Login">

</form>
</body>
</html>

<--------------------------------------------------->
date.jsp

<html>
<head>
<title>Time and date</title>
<meta name=
"author" content="Fred Flintstone">
</head>
<body>
Right now, it's:<br>
<b><%=
new java.util.Date().toString() %></b>
</body>
</html>



其他:1 对于页面可将公共代码重构到应用文件中来简化代码和重用代码
2
对于页面可以使用样式表做的更彻底,使页面的主体部分不会淹没带大量的结构代码中

 

 

SiteMesh 优雅地分离页面的内容与框架

 

SiteMesh 优雅地分离页面的内容与框架

DE> DE>

DE>http://www.matrix.org.cn/blog/javen/archives/000695.htmlDE>

J 友的推荐,试下 sitemesh ,一个字:

看过不少的开源框架、Util包,感觉 sitemesh 是最令人眼前一亮的: 非常之简单,而解决了很重要的问题,真正实现页面显示 view 的内容与框架(或者说布局、导航)的分离。这就是设计啊! 感觉到设计的美了!

sitemesh 解决的问题是: 通常你用 include 时做的事情。高级一点就是简单的 template 标签。如果用 struts 的话,不少人用它的 tiles ,就是解决页面布局。

sitemesh 现在的作法是,你原来用 include 的地方现在都不需要了。你做的所有内容页面,就是页面本身就好了,完整的页面本身,不需要 include 进来布局、导航,比如通常 header / footer 之类。 sitemesh 使用 servlet filter 技术,在一个页面请求 response 回来时(这时不管你用哪种 动态语言 asp / php / jsp 等,都已经生成了 html ),对页面进行解析,并为该页面 decorator 上布局、导航。

这又是“分离”的思想。最近学点 IoC ,包括 spring 之类的容器就一直在理解这个理念。 sitemesh 这里的分离,使得内容页面,不知道自己将被怎么样地被“装饰”,而这个装饰由容器通过配置文件来控制。 这有如 IoC 容器做的事情,它使得对象本身不需要知道自己将被怎么样被调用。

decorator 即装饰,熟悉设计模式的朋友知道有这么个设计模式。而 sitemesh 就是这个设计模式的一个实现。

说实话这个模式我没有特别去理解,也没有用过。 我感觉 sitemesh 的成功之处在于,一个非常独到的设计,即对 html 文件的分析。 而要实现对 html 的分析,就要借助于 servlet filter 技术了。 的确 filter 技术出来后,有很多的事情借助它来实现就轻松多了。又想起 Interceptor/AOP ,可以在任何你需要的地方拦截,加入自己的代码。

技术的发展更新也是太快了,同样是 Java 也有不少年了,还是能变出不少的花样来。好好学习呀,天天向上!

想要我推荐 sitemesh 的中文资料吗? google sitemesh



感觉还没有说完,继续 sitemesh 的好处:
1
、与 control 无关,不管你直接使用 servlet 做控制,还是使用 struts / webwork 之类的框架,都是完全无关的。 因为 sitemesh 实际上只是 filter
2
、多 view 的支持。当然 jsp 的支持是很轻松的。 php/asp 等动态页面语言也可以支持,因为 sitemesh 实际上处理的对象是 html 文件。 velocity / freemarker 等模板语言也有支持,这个需要特别的设置

3
、非常非常地简单易用。保证 30 分钟学会。 如果只是一般地使用,只需要使用看下 example ,使用默认的配置文件就搞定了
4
、使用 sitemesh 后,内容页面就非常地单纯,有利于美工做页面设计
注:上面所有的努力,都是: 走过路过不要错过!

javen 发表于 October 7, 2004 12:38 AM | 引用

 

 

 

AppFuse里消息显示机制

 

AppFuse里消息显示机制

作者: qinhui99(http://qinhui99.itpub.net)

发表于: 2005.07.24 19:59

分类: appfuse

出处: http://qinhui99.itpub.net/post/2570/35966

---------------------------------------------------------------

 

Appfuse里面,使用了sitemeshstruts来显示操作错误信息和成功信息。其中,sitemesh用来控制消息显示的风格和样式,strutsaction产生消息。

 

1 Sitemesh的介绍

Sitemeshhttp://www.opensymphony.com/sitemesh/)是一款开源的布局管理的framework。配置比较简单,功能也不错。Sitemesh除了有java版外,也有一个基于.net的版本(http://sourceforge.net/cvs/?group_id=26435)。

 

 

简单的说,Sitemesh是一个网站的布局管理器。Sitemesh可以帮助开发者定义网站整体布局。包括:定义网站由几部分组成;每部分内容位置;每部分的内容;每部分的风格等等。

 

 

从原理上说,sitemesh使用了filter,把request拦截住,然后把预定义的布局与响应的内容包装起来(采用了decorator模式),再response给客户。

 

2 错误和成功消息的显示

page目录下的message.jsp里面,定义了错误信息显示的方式和成功信息显示的方式。如下:

 

<%-- Error Messages --%>

 

<! logic:messagesPresent标签的使用请参考struts的用户手册。 à

 

<logic:messagesPresent>

 

<div class="error">

 

<html:messages id="error">

 

<html:img pageKey="icon.warning.img"

 

altKey="icon.warning" styleClass="icon"/>

 

<bean:write name="error" filter="false"/><br/>

 

</html:messages>

 

</div>

 

</logic:messagesPresent>

 

 

<%-- Success Messages --%>

 

<logic:messagesPresent message="true">

 

<div class="message">

 

<html:messages id="message" message="true">

 

<html:img pageKey="icon.information.img"

 

altKey="icon.information" styleClass="icon"/>

 

<bean:write name="message" filter="false"/><br/>

 

</html:messages>

 

</div>

 

</logic:messagesPresent>

 

 

在定义了错误信息显示的方式和成功信息显示的方式后,开发者只需要在action里面设置ActionMessage对象就可以显示相关的信息了。下面就举个例子:

 

public class ComanyMgrAction extends BaseAction

 

{

 

..

 

 

public ActionForward execute(ActionMapping mapping, ActionForm form,

 

HttpServletRequest request,

 

HttpServletResponse response)

 

throws Exception

 

{

 

String username;

 

//定义错误信息集合和成功信息集合

 

ActionMessages errors = new ActionMessages();

 

ActionMessages messages = new ActionMessages();

 

 

//收集成功信息

 

If(checkUser(username))

 

{

 

messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(“legal.user”

 

"合法的用户!"));

 

saveMessages(request, messages);

 

return mapping.findForward("success");

 

}

 

else // 收集错误信息

 

{

 

errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(

 

"errors.user", “非法用户!”));

 

saveErrors(request, errors);

 

return mapping.findForward("error");

 

}

 

}

 

 

}

 

 

在相对应的jsp页面里面,必须有一个content的元素。例如,

 

<content>

 

..

 

</content>

 

 

这样从action返回的成功信息或错误信息就会在content里自动显示。用起来真的很方便。

 

提示:当开发者需要创建新的jsp页面时,最好拷贝appfuse自己产生的jsp页面来创建。

0 0
原创粉丝点击