SiteMesh框架的用法。。。。。

来源:互联网 发布:苹果手机下安卓软件 编辑:程序博客网 时间:2024/05/16 12:21

SiteMesh框架是OpenSymphony团队开发的一个非常优秀的页面装饰器框架,它通过对用户请求进行过滤,并对服务器向客户端响应也进行过滤,然后给原始页面加入一定的装饰(header,footer等),然后把结果返回给客户端。通过SiteMesh的页面装饰,可以提供更好的代码复用,所有的页面装饰效果耦合在目标页面中,无需再使用include指令来包含装饰效果,目标页与装饰页完全分离,如果所有页面使用相同的装饰器,可以是整个Web应用具有统一的风格。

下面是一个简单的例子:

将sitemesh-2.4.jar放 到 [web-app]/WEB-INF/lib目录下;

在[web-app]/WEB-INF/新建一个decorators.xml文件,配置如下:

 

<?xml version="1.0" encoding="UTF-8"?><decorators defaultdir="/WEB-INF/decorators">    <!-- Any urls that are excluded will never be decorated by Sitemesh -->    <excludes>        <pattern>/exclude.jsp</pattern>        <pattern>/exclude/*</pattern>    </excludes>    <decorator name="main" page="main.jsp">        <pattern>/*</pattern>    </decorator></decorators><!--不要忘了在web.xml文件配置一下:-->  <filter>  <filter-name>sitemesh</filter-name>  <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>  </filter>  <filter-mapping>  <filter-name>sitemesh</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping><?xml version="1.0" encoding="UTF-8"?><decorators defaultdir="/WEB-INF/decorators">    <!-- Any urls that are excluded will never be decorated by Sitemesh -->    <excludes>        <pattern>/exclude.jsp</pattern>        <pattern>/exclude/*</pattern>    </excludes>    <decorator name="main" page="main.jsp">        <pattern>/*</pattern>    </decorator></decorators>不要忘了在web.xml文件配置一下:  <filter>  <filter-name>sitemesh</filter-name>  <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>  </filter>  <filter-mapping>  <filter-name>sitemesh</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping><?xml version="1.0" encoding="UTF-8"?><decorators defaultdir="/WEB-INF/decorators">    <!-- Any urls that are excluded will never be decorated by Sitemesh -->    <excludes>        <pattern>/exclude.jsp</pattern>        <pattern>/exclude/*</pattern>    </excludes>    <decorator name="main" page="main.jsp">        <pattern>/*</pattern>    </decorator></decorators>不要忘了在web.xml文件配置一下:  <filter>  <filter-name>sitemesh</filter-name>  <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>  </filter>  <filter-mapping>  <filter-name>sitemesh</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping>


 

下面是jsp页面代码

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %><%@taglib prefix="s" uri="/struts-tags" %><!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><decorator:title default="欢迎使用文档管理系统"/></title><link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/main.css"/><decorator:head/></head><body><s:if test="#session.loginUser!=null">欢迎[${loginUser.nickname }]登录我们的系统 <s:if test="#session.loginUser.type==1"><a href="<%=request.getContextPath()%>/user_list.action">用户管理</a><a href="<%=request.getContextPath()%>/department_list.action">部门管理</a></s:if><a href="<%=request.getContextPath()%>/document_listReceive.action">公文管理</a><a href="<%=request.getContextPath()%>/message_listReceive.action">私人信件</a><a href="<%=request.getContextPath()%>/user_updateSelfInput.action?id=${loginUser.id}">修改个人信息</a> <a href="<%=request.getContextPath()%>/user_showSelf.action?id=${loginUser.id}">个人信息查询</a> <a href="<%=request.getContextPath()%>/user_addEmailInput.action">绑定邮箱</a> <a href="<%=request.getContextPath()%>/login!logout.action">退出系统</a></s:if><s:else><a href="<%=request.getContextPath()%>/login!loginInput.action">用户登录</a></s:else><hr/><h3 align="center"><decorator:title default="文档管理系统"/></h3><decorator:body/><div align="center" style="width:100%;border-top:1px solid; float:left;margin-top:10px;">CopyRight@2013-2016<br/>欢迎浏览本网站!谢谢使用!</div></body></html><%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %><%@taglib prefix="s" uri="/struts-tags" %><!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><decorator:title default="欢迎使用文档管理系统"/></title><link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/css/main.css"/><decorator:head/></head><body><s:if test="#session.loginUser!=null">欢迎[${loginUser.nickname }]登录我们的系统 <s:if test="#session.loginUser.type==1"><a href="<%=request.getContextPath()%>/user_list.action">用户管理</a><a href="<%=request.getContextPath()%>/department_list.action">部门管理</a></s:if><a href="<%=request.getContextPath()%>/document_listReceive.action">公文管理</a><a href="<%=request.getContextPath()%>/message_listReceive.action">私人信件</a><a href="<%=request.getContextPath()%>/user_updateSelfInput.action?id=${loginUser.id}">修改个人信息</a> <a href="<%=request.getContextPath()%>/user_showSelf.action?id=${loginUser.id}">个人信息查询</a> <a href="<%=request.getContextPath()%>/user_addEmailInput.action">绑定邮箱</a> <a href="<%=request.getContextPath()%>/login!logout.action">退出系统</a></s:if><s:else><a href="<%=request.getContextPath()%>/login!loginInput.action">用户登录</a></s:else><hr/><h3 align="center"><decorator:title default="文档管理系统"/></h3><decorator:body/><div align="center" style="width:100%;border-top:1px solid; float:left;margin-top:10px;">CopyRight@2013-2016<br/>欢迎浏览本网站!谢谢使用!</div></body></html>


 

 


原创粉丝点击