decorators.xml的用法


簡介:
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>
   
   
  
  
          
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

二、在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>
   
   
  
  
          
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

其中
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>
   
   
  
  
          
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

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>
   
   
  
  
          
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

其中<%@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>
......
   
   
  
  
          
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

以上是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">
   
   
  
  
          
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

其中content=“default”中的“default”對應“decorators.xml”配置文件中的“ name”屬性值。
通過上面的配置,在加載完的頁面,點擊右鍵查看源代碼可以看到加載完頁面會出現head.jsp頁面的代碼。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM