最近刚刚去了一家新公司,由于不熟悉代码结构,导致我找一个jsp文件都十分费劲,后来经过了一系列的找啊找,终于发现了一个jsp的拼装框架Sitemesh,这个框架非常精巧,仅用于jsp的拼装,使得view层的代码很整洁,接下来,我把我学习Sitemesh的过程总结一下,与大家分享。
一、Sitemesh简介 SiteMesh是由一个基于Web页面布局、装饰以及与现存Web应用整合的框架。它能帮助我们在由大量页面构成的项目中创建一致的页面布局和外观,如一致的导航条,一致的banner,一致的版权,等等。 它不仅仅能处理动态的内容,如jsp,php,asp等产生的内容,它也能处理静态的内容,如htm的内容,使得它的内容也符合你的页面结构的要求。甚至于它能将HTML文件象include那样将该文件作为一个面板的形式嵌入到别的文件中去。所有的这些,都是GOF的Decorator模式的最生动的实现。尽管它是由java语言来实现的,但它能与其他Web应用很好地集成。与传统区别如下图:
二、Sitemesh工作原理 Sitemesh框架是OpenSymphony团队开发的一个非常优秀的页面装饰器框架,它通过对用户请求进行过滤,并对服务器向客户端响应也进行过滤,然后给原始页面加入一定的装饰(header,footer等),然后把结果返回给客户端。通过SiteMesh的页面装饰,可以提供更好的代码复用,所有的页面装饰效果耦合在目标页面中,无需再使用include指令来包含装饰效果,目标页与装饰页完全分离,如果所有页面使用相同的装饰器,可以是整个Web应用具有统一的风格。
三、写一个Demo来使用Sitemesh 1.引入struts2相关jar包与sitemesh 2.3.jar 2.配置web.xml <filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>
com.opensymphony.module.sitemesh.filter.PageFilter
</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*.html</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> 3.写一个简单的struts2的action: public class NewInvoice extends ActionSupport {
@Override
public String execute() throws Exception {
return this.SUCCESS;
}
} 4.配置文件struts.xml: <?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE struts PUBLIC
'-//Apache Software Foundation//DTD Struts Configuration 2.3//EN'
'http://struts./dtds/struts-2.3.dtd'>
<struts>
<!-- 开发模式,修改后自动重启服务器 -->
<constant name='struts.devMode' value='true' />
<!-- 设置编码,解决中文问题 -->
<constant name='struts.i18n.encoding' value='UTF-8' />
<package name='default' namespace='/' extends='struts-default'>
<action name='NewInvoice' class='struts2.web.action.NewInvoice'>
<result name='success'>/success.jsp</result>
</action>
</package>
</struts>
5.在src下new一个 decorators.xml,这个是sitemesh的装饰器配置文件。
<?xml version='1.0' encoding='UTF-8'?>
<decorators defaultdir='/decorators'>
<decorator name='main' page=“decoratePage1.jsp '>
<pattern>/NewInvoice.action</pattern>
</decorator>
<decorator name=“pop ' page=“decoratePage2.jsp '>
<pattern>/show*.do</pattern>
<pattern>/*.jsp</pattern>
</decorator>
</decorators>
6.new一个decoratePage1.jsp作为装饰器模版: <%@page contentType='text/html;?charset=GBK'%>
<%@taglib uri='sitemesh-decorator' ?prefix='decorator'%>
<html>
<head>
<title><decorator:title /></title>
<decorator:head />
</head>
<body>
Hello World
<hr />
<decorator:body />
</body>
</html>
7.new一个success.jsp <%@ page contentType='text/html;?charset=GBK'%>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<p>Decorated page goes here.</p
</body>
</html> 至此,sitemesh的Demo代码写完了。总结一下,首先通过web.xml里面配置的sitemesh的filter进行url请求过滤,根据不同的action请求,找到不同的装饰器模版,将装饰器模版加到结果页面中。这就是sitemesh框架,现在你知道怎么用了吗? 如果有疑问欢迎在下面留言,谢谢。 |
|