com.opensymphony.module.sitemesh.filter.PageFilter页面装饰器

来源:互联网 发布:类似matlab的软件 编辑:程序博客网 时间:2024/06/05 07:08

         1.sitemesh简介

           sitemesh是由一个基于web页面布局、装饰以及与现存web应用整合的框架。它能帮助我们在由大量页面构成的项目中创建一致的页面布局和外 观,如一致的导航条,一致的banner,一致的版权,等等。它不仅仅能处理动态的内容,如jsp,php,asp等产生的内容,它也能处理静态的内容, 如htm的内容,使得它的内容也符合你的页面结构的要求。甚至于它能将html文件象include那样将该文件作为一个面板的形式嵌入到别的文件中去。 所有的这些,都是gof的decorator模式的最生动的实现。尽管它是由java语言来实现的,但它能与其他web应用很好地集成。

      2.sitemesh原理

          一个请求到服务器后,如果该请求需要sitemesh装饰,服务器先解释被请求的资源,然后根据配置文件获得用于该请求的装饰器,最后用装饰器装饰被请求资源,将结果一同返回给客户端浏览器。

    3.源码

------案例一:

         A.首先要下载包,本次用的是sitemesh-2.4.1.jar

        B.新建项目,我的项目名:sitemesh

        C.配置,web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

                    xmlns="http://java.sun.com/xml/ns/javaee"

                    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

                   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
  <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>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>    

       D.配置:decorators.xml    (这里和web.xml是同一级目录)

<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/WEB-INF/decorators">       //defaultdir: 包含装饰器页面的目录   用来存放装饰器jsp目录,即在这里存在的是mydecorator1.jsp
    <decorator name="mydecorator1" page="mydecorator1.jsp">
        <pattern>/test1.jsp</pattern>           //被装饰的页面
    </decorator>
</decorators>    

              ---------------------------------------mydecorator1.jsp页面内容

 <%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<html>
    <head>
        <title>My Site - <decorator:title default="Welcome to sitemesh!" /></title>
        <decorator:head />
    </head>
    <body>
        <decorator:body />
        <p>This message is in /decorators/mydecorator1.jsp</p>       
    </body>
</html>

------------------------test1.jsp内容

 <%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>This is test1 jsp</title>
    </head>
    <body>
    <b>This is test1</b>
    </body>
</html>

--------------------------运行效果如下:

------案例二:(将test1.jsp中的title标签清空,即不放内容):

效果:

-----------------------------案例三  <decorator:getProperty property=""/>标签

-------------mydecorator2.jsp页面

<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<html>
    <head>
        <title>My Site - <decorator:title default="Welcome!" /></title>
        <decorator:head />
    </head>

    <body>
        <decorator:body />

        <decorator:getProperty property="page.content1"/>
        <decorator:getProperty property="page.content2"/>
        <decorator:getProperty property="page.content4"/>
       
        <!-- do nothing -->
        <decorator:getProperty property="page.content3"/>
       
        <p>This message is in /decorators/mydecorator2.jsp</p>
    </body>
</html>

---------------------test2.jsp页面

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>This is test2</title>
    </head>
   
    <body>
    <b>This is test2</b>
    <b>Use &lt;decorator:getProperty&gt; tag</b>
   
    <content tag="content1"><p>This is content1</p></content>
    <content tag="content2"><p>This is content2</p></content>
    <content tag="content4"><p>This is content4, it shouldn't be display</p></content>
    </body>
</html>

----------------decorators.xml内容

<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/WEB-INF/decorators">
<!--  
    <decorator name="mydecorator1" page="mydecorator1.jsp">
        <pattern>/test1.jsp</pattern>
    </decorator>
-->
    <decorator name="mydecorator2" page="mydecorator2.jsp">
        <pattern>/test2.jsp</pattern>
    </decorator>
</decorators>

效果如下:


------案例四:


            

原创粉丝点击