sitemesh3配置

来源:互联网 发布:崩坏学园淘宝店 编辑:程序博客网 时间:2024/06/06 00:43

下面是配置过程:

1. 新建一个web工程名字sitemesh3
2. 添加sitemesh3的jar到lib文件夹中
3. 在web.xml中添加:
 <filter>
  <filter-name>sitemesh</filter-name>
  <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
 </filter>

 <filter-mapping>
  <filter-name>sitemesh</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

4. 新建一个装饰页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><!-- 这里就是装饰页面 -->
  <head>
    <title><sitemesh:write property="title"/></title>
    <sitemesh:write property="head"/>
    <style type="text/css">
     body{font-weight: bold;color: red;}
    </style>
    <script type="text/javascript">
     function f(){
      alert("我来自装饰页面");
     }
    </script>
  </head>
  <body>
   我是装饰页面,下面是被装饰页面的内容:<br>
    <sitemesh:write property="body"/>
  </body>
</html>

这里的装饰页面可以是html也可以是jsp

5. 新建一个index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
</head>
<body>
 welcome!
 <button onclick="f()">点我</button><br> 
</body>
</html>

6. 在WEB-INF下新建一个sitemesh3.xml文件:
<sitemesh>
 <!-- 根目录下的所有页面让 decorator.html装饰-->
 <mapping path="/*" decorator="/decorator/decorator.html" />
</sitemesh>

    6.1 如果我们有一张页面不需要被装饰,那么可以这样定义:

<mapping path="/javadoc/page.html" exclue="true"/>

也就是说在javadoc文件夹下面的page.html文件不会被装饰

如果想要整个文件夹下的页面都不被装饰可以用*代替:

<mapping path="/javadoc/*" exclue="true"/>

    6.2 如果我们想要javadoc文件夹下的文件被多个装饰页面装饰,可以这样配置:

<mapping>

   <path>/javadoc/*</path>

  <decorator>/decorators/article.html</decorator>

  <decorator>/decorators/two-page-layout.html</decorator>

  <decorator>/decorators/common.html</decorator>

</mapping> 

这里javadoc下的所有页面被三个装饰页面装饰

7. 运行程序访问http://localhost/sitemesh3/index.jsp

 

程序截图: 

  http://blog.csdn.net/thc1987/article/details/6913416

 demo:点击打开链接

0 0