首页 > 代码库 > sitemesh学习笔记(1)

sitemesh学习笔记(1)

最近在学习web开发的时候,发现很多的页面都存在同样的导航栏,登陆栏,js,jQuery等等相同的元素。这样就感觉开发变得好臃肿啊,并且,有时候改一个元素,就要把所有包含这个元素的页面全部重新码一遍,亲们,是不是觉得很麻烦啊,我也觉得很麻烦。所以快来使用sitemesh框架吧,哈哈。

下面我做了一个demo,来基本介绍一下怎么使用这个框架。

首先我用myeclipse建一个java web工程,起名叫sitemesh,然后将sitemesh-2.x.jar copy到lib下面,这样就有了一个官方提供的包了,然后我们当然是去配置一些东西了。在WEB-IFN下面新建一个decorators.xml:

<?xml version="1.0" encoding="utf-8"?>  <!-- 定一个一个默认的装饰包--><decorators defaultdir="/decorators">     <!-- 此处用来定义不需要过滤的页面 -->      <excludes>      </excludes>     <!-- 用来定义装饰器要过滤的页面 列出装饰页面-->      <decorator name="main" page="main.jsp">          <pattern>/*</pattern>      </decorator>  <!-- 指明装饰页面的根目录,和用来装饰的文件名--></decorators>  

在WEBRoot下面在建立一个文件夹decorators作为一个装饰包,然后在这个文件夹下面建立一个main.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="http://www.mamicode.com/">            <title><decorator:title default="默认title"/></title>      	<meta http-equiv="pragma" content="no-cache">	<meta http-equiv="cache-control" content="no-cache">	<meta http-equiv="expires" content="0">    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">	<meta http-equiv="description" content="This is my page">	<!--	<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css">	-->	<decorator:head/>    </head>    <body>   <h2>SiteMesh装饰header</h2>         <hr />      <!-- 从被装饰页面获取body标签内容 -->     <decorator:body />         <hr />      <h2>SiteMesh装饰footer</h2>    </body></html>

一些sitemesh的标签获得被装饰页面的元素,这里也许有些亲们要问这样一个问题了,装饰页面不是应该被送到被装饰页面然后组合吗?为什么装饰页面反而得到被装饰页面的内容呢?有这样的问题的朋友,等我讲完估计就明白了。

接下配置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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>sitemesh</display-name>  <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.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list></web-app>

主要就是添加中间的filter过滤器,这里叫装饰器吧,定义了一个装饰器的类包,就是官方提供了jar包啦,有了jar包,有了装饰器,当然还需要一个被装饰页面拉,

<url-pattern>/*</url-pattern> 就指定了被装饰页面的路径。其实也就是app根目录了。

光指明路径还不够啊,怎么也得定一个jsp,所以在WEBRoot下面建立一个index.jsp. myeclipse已经自动帮你建了。

然后添加如下内容:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="http://www.mamicode.com/">        <title>被装饰(目标)页面title</title>	<meta http-equiv="pragma" content="no-cache">	<meta http-equiv="cache-control" content="no-cache">	<meta http-equiv="expires" content="0">    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">	<meta http-equiv="description" content="This is my page">	<!--	<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css">	-->  </head>    <body><h4>被装饰(目标)页面body标签内内容。</h4>   <h3>使用SiteMesh的好处?</h3>   <ul>       <li>被装饰(目标)页面和装饰页面完全分离。</li>       <li>做到真正的页面复用,一个装饰页面装饰多个被装饰(目标)页面。</li>        <li>更容易实现统一的网站风格。</li>        <li>还有。。。</li>       </ul>    </body></html>

然后开启你的服务器,打开你的浏览器键入:localhost:8080/sitemesh/index.jsp。看到结果了吗?结果就是两个页面合体了。

回到刚刚那个问题,为什么装饰页面要得到被装饰页面的内容?

容我讲述一遍,框架运行的步骤,当你键入上述地址的时候,服务器得到一个请求,要求访问index.jsp页面,服务器收到通知以后先去读web.xml,发现有index.jsp页面需要装饰,装饰器叫sitemesh,然后在去读decorators.xml得到装饰页面的文件路径,然后呢执行这个装饰页面,通过这个sitemesh提供的标签,得到被装饰页面的元素,这个时候的装饰页面就成了一个结合体,然后把这个结合体发送给你,浏览器得到这个结合体的代码。当然拉,这个装饰页面肯定是多线程的啊,不然不是只能用一次了。它是可以被同时反复地使用的。