sitemesh decorators.xml的用法

转载:https://blog.****.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标签在每个jsp页面中来不断的包含各种header, stylesheet, scripts and footer
    <%@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>

sitemesh decorators.xml的用法

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

 

以上是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页面的代码。