原文地址:https://blog.csdn.net/laozhuxiao/article/details/54342121

简介:
sitemesh应用Decorator模式,用filter截取request和response,把页面组件head,content,banner结合为一个完整的视图。通常我们都是用include标签在每个jsp页面中来不断的包含各种header, stylesheet, scripts and footer,现在,在sitemesh的帮助下,我们可以开心的删掉他们了
一、在WEB-INF/web.xml中copy以下filter的定义:

<filter>
    <filter-name>sitemeshFilter</filter-name>
    <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>sitemeshFilter</filter-name>
    <url-pattern>/a/*</url-pattern>
</filter-mapping>

二、在WEB-INF下,建立一个decorators.xml配置文件,如下:

<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/static">
    <!-- 默认装饰页面, 在需要装饰的页面增加<meta name="decorator" content="default"/> -->
    <decorator name="blank" page="layouts/blank.jsp" />
    <decorator name="default" page="layouts/default.jsp" />
</decorators>

其中
blank.jsp页面代码如下:

<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/static/include/taglib.jsp"%>
<%@ taglib prefix="sitemesh" uri="http://www.opensymphony.com/sitemesh/decorator" %>
<!DOCTYPE html>
<html style="overflow-x:auto;overflow-y:auto;">
<head>
    <title><sitemesh:title/></title><!--  - Powered By JeeSite -->
    <%@include file="/static/include/head.jsp" %>
    <sitemesh:head/>
</head>
<body>
    <sitemesh:body/>
</body>
</html>

default.jsp代码如下:

<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/static/include/taglib.jsp"%>
<%@ taglib prefix="sitemesh" uri="http://www.opensymphony.com/sitemesh/decorator" %>
<!DOCTYPE html>
<html style="overflow-x:auto;overflow-y:auto;">
<head>
    <title><sitemesh:title/></title>
    <%@include file="/static/include/head.jsp" %>       
    <sitemesh:head/>
</head>
<body id="<sitemesh:getProperty property='body.id'/>" class="<sitemesh:getProperty property='body.class'/>"  style="<sitemesh:getProperty property='body.style'/>">
    <sitemesh:body/>
    <script type="text/javascript">//<!-- 无框架时,左上角显示菜单图标按钮。
        if(!(self.frameElement && self.frameElement.tagName=="IFRAME")){
            $("body").prepend("<i id=\"btnMenu\" class=\"icon-th-list\" style=\"cursor:pointer;float:right;margin:10px;\"></i><div id=\"menuContent\"></div>");
            $("#btnMenu").click(function(){

top.layer.open({
                    type: 2,
                    area:['300px','350px'],
                    content: 'get:${ctx}/sys/menu/treeselect;JSESSIONID=<shiro:principal property="sessionid"/>' //这里content是一个URL,如果你不想让iframe出现滚动条,你还可以content: ['http://sentsin.com', 'no']
                });
                //top.$.jBox('get:${ctx}/sys/menu/treeselect;JSESSIONID=<shiro:principal property="sessionid"/>', {title:'选择菜单', buttons:{'关闭':true}, width:300, height: 350, top:10});
                //if ($("#menuContent").html()==""){$.get("${ctx}/sys/menu/treeselect", function(data){$("#menuContent").html(data);});}else{$("#menuContent").toggle(100);}
            });
        }//-->
    </script>
</body>
</html>

其中<%@include file=”/static/include/head.jsp” %>中的head.jsp页面就是所有js或者css公用引用的页面,例如:

<%@ page contentType="text/html;charset=UTF-8" %><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><meta name="author" content="http://www.jeeplus.org/"/>
<meta name="renderer" content="webkit"><meta http-equiv="X-UA-Compatible" content="IE=9,IE=10" />
<meta http-equiv="Expires" content="0"><meta http-equiv="Cache-Control" content="no-cache"><meta http-equiv="Cache-Control" content="no-store">
<!-- 引入jquery插件 -->
<script src="${ctxStatic}/jquery/jquery-2.1.1.min.js" type="text/javascript"></script>
<script src="${ctxStatic}/jquery/jquery-migrate-1.1.1.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="${ctxStatic}/TableDnD/jquery.tablednd.js"></script>
<!-- 引入依赖的第三方插件 -->
<script src="${ctxStatic}/slimscroll/jquery.slimscroll.min.js"></script>
<script src="${ctxStatic}/jquery-validation/1.14.0/jquery.validate.min.js" type="text/javascript"></script>
<script src="${ctxStatic}/jquery-validation/1.14.0/localization/messages_zh.min.js" type="text/javascript"></script>
<script src="${ctxStatic}/jquery-validation/1.14.0/additional-methods.min.js" type="text/javascript"></script>
<link href="${ctxStatic}/jquery-jbox/2.3/Skins/Bootstrap/jbox.min.css" rel="stylesheet" />
<script src="${ctxStatic}/jquery-jbox/2.3/jquery.jBox-2.3.min.js" type="text/javascript"></script>
<script src="${ctxStatic}/pace/pace.min.js"></script>
<script src="${ctxStatic}/metisMenu/jquery.metisMenu.js"></script>
......

以上是decorators.xml主要配置文件,根据这个配置文件的注释可以看出,需要在每一个需要引用公共js或者css的地方添加注释的一行代码,如下:

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/static/include/taglib.jsp"%>
<html>
<head>
    <title>通知管理</title>
    <!-- 这一行代码是重点 -->
    <meta name="decorator" content="default"/>

</head>
<body class="gray-bg">

其中content=“default”中的“default”对应“decorators.xml”配置文件中的“ name”属性值。
通过上面的配置,在加载完的页面,点击右键查看源代码可以看到加载完页面会出现head.jsp页面的代码。

[转]decorators.xml的用法的更多相关文章

  1. 转-decorators.xml的用法-http://blog.csdn.net/gavinloo/article/details/7458062

    今天改前人做的项目,用struts2,spring,hibernate框架做的,对了,还有jQuery.我用jquery做异步请求到后台,生成json数据返回前台生成下拉输入框,请求到后台以后,成功生 ...

  2. decorators.xml的用法 (转)

    spring,hibernate框架做的,对了,还有jQuery. 我用jquery做异步请求到后台,生成json数据返回前台生成下拉输入框,请求到后台以后,成功生成了json数据并根据struts的 ...

  3. decorators.xml的用法

    spring,hibernate框架做的,对了,还有jQuery.我用jquery做异步请求到后台,生成json数据返回前台生成下拉输入框,请求到后台以后,成功生成了json数据并根据struts的映 ...

  4. SQL FOR XML PATH 用法

    FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作.那么以一个实例为主 ...

  5. 装饰页面decorators.xml

    WEB-INF/decorators.xml 这个配置可以增加页面的 装饰页面

  6. 一个由正则表达式引发的血案 vs2017使用rdlc实现批量打印 vs2017使用rdlc [asp.net core 源码分析] 01 - Session SignalR sql for xml path用法 MemCahe C# 操作Excel图形——绘制、读取、隐藏、删除图形 IOC,DIP,DI,IoC容器

    1. 血案由来 近期我在为Lazada卖家中心做一个自助注册的项目,其中的shop name校验规则较为复杂,要求:1. 英文字母大小写2. 数字3. 越南文4. 一些特殊字符,如“&”,“- ...

  7. 使用Spring Mvc 转发 带着模板 父页面 之解决方法 decorators.xml

    周末了,周一布置的任务还没完毕,卡在了页面跳转上,接手了一个半截的项目要进行开发,之前没有人给培训.全靠自己爬代码,所以进度比較慢.并且加上之前没实用过 Spring Mvc 开发项目.所以有点吃力, ...

  8. mssql sqlserver for xml EXPLICIT 用法详解说明

    摘要:下文通过举例的方式,详细说明"for xml EXPLICIT"关键字的用法,如下所示:实验环境:sql server 2008 R2 EXPLICIT的功能:将数据表采用特 ...

  9. sql 将某列转换成一个字符串 for xml path用法

    declare @test table( name varchar(10)) insert into @test values('a') insert into @test values('b') i ...

随机推荐

  1. Vue项目初始

    利用npm搭建Vue项目流程 安装 第一步: 官方下载node 或者pip install node 第二步:可忽略 :  npm install npm@latest -g 更新最新的稳定版本 第三 ...

  2. js变量和函数声明的提升

    函数声明和变量声明总是会被解释器悄悄地被“提升”到方法体的最顶部 请注意,变量赋值并没有被提升,只是声明被提升了. 函数的声明比变量的声明具有高的优先级. 下面的程序是什么结果? var foo =  ...

  3. android 职业 转行

    不知道多少人和我一样.学安卓安卓工作.成了一件很烦躁的事情.甚至迷茫.    起初学安卓,是因为安卓流行,所以有兴趣,想要学.那个时候想做一个应用,想对安卓手机有个了解,比如获取手机短信.没有太在意工 ...

  4. 解决SpringBoot的@Autowired无法注入问题

    问题:@Autowired无法自动注入 思路:SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描!"Application类"是指 ...

  5. express框架中如何只执行一次res响应操作

    在做东西时候遇到一个可能会重复输出res.json的地方,重复输出会产生Error:Cannot set header after they are sent. Node.js不像c++里可以直接通过 ...

  6. Codeforces Round #538 (Div. 2)

    目录 Codeforces 1114 A.Got Any Grapes? B.Yet Another Array Partitioning Task C.Trailing Loves (or L'oe ...

  7. 51nod 算法马拉松30

    题目链接 附一个代码地址 A,这个容斥一下就好了 C,rxd大爷给讲的,首先如果分三种情况(成环,正在比配环,未访问)讨论复杂度是\(3^n * n ^ 2\)的,但是对于每一个环,都可以直接枚举环的 ...

  8. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  9. 2018 完美搭建VS Code 的JAVA开发环境并解决print乱码问题

    出自微软的Visual Studio Code 并不是一个 IDE,它是个有理想,有前途的编辑器,通过相应语言的插件,可以将其包装成一个 轻量级的功能完善的IDE. 自从遇见了她,真的是对她一见钟情不 ...

  10. JS 之 script标签

    1.script标签 1.js代码的解析(包括下载js文件)会阻塞页面加载 2.当js文件放在头部,页面必须等所有js代码都被下载,解析和执行完成后才开始呈现页面内容(遇到body标签才呈现),对于那 ...