前端之家收集整理的这篇文章主要介绍了
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>
url-pattern>/a/*
url-pattern>
filter-mapping>
二、在WEB-INF下,建立一个decorators.xml配置文件,如下:
1
2
3
4
5
6
<?xml version="1.0" encoding="UTF-8"?>
decorators defaultdir=
"/static">
decorator name=
"blank" page=
"layouts/blank.jsp" />
"default" "layouts/default.jsp" />
decorators>
其中
blank.jsp页面代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
<%@ 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>
sitemesh:body/>
body>
html>
default.jsp代码如下:
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
- 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
<%@ page contentType=title>
<%@include file="/static/include/head.jsp" %>
body id=
"<sitemesh:getProperty property='body.id'/>" class=
"<sitemesh:getProperty property='body.class'/>" "<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"/>' }); }); } script>
其中<%@include file=”/static/include/head.jsp” %>中的head.jsp页面就是所有js或者css公用引用的页面,例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
<%@ page contentType="text/html;charset=UTF-8" %>Meta http-equiv="Content-Type" content="text/html;charset=utf-8" />"author" "http://www.jeeplus.org/"/>
"renderer" "webkit">
"X-UA-Compatible" "IE=9,IE=10" />
"Expires" "0">
"Cache-Control" "no-cache">
"no-store">
src=
"${ctxStatic}/jquery/jquery-2.1.1.min.js" "text/javascript">
"${ctxStatic}/jquery/jquery-migrate-1.1.1.min.js" script>
"text/javascript" "${ctxStatic}/TableDnD/jquery.tablednd.js">
script>
"${ctxStatic}/slimscroll/jquery.slimscroll.min.js">
"${ctxStatic}/jquery-validation/1.14.0/jquery.validate.min.js" "${ctxStatic}/jquery-validation/1.14.0/localization/messages_zh.min.js" "${ctxStatic}/jquery-validation/1.14.0/additional-methods.min.js" link href=
"${ctxStatic}/jquery-jBox/2.3/Skins/Bootstrap/jBox.min.css" rel=
"stylesheet" />
"${ctxStatic}/jquery-jBox/2.3/jquery.jBox-2.3.min.js" "${ctxStatic}/pace/pace.min.js">
"${ctxStatic}/metisMenu/jquery.metisMenu.js">
script>
......
以上是decorators.xml主要配置文件,根据这个配置文件的注释可以看出,需要在每一个需要引用公共js或者css的地方添加注释的一行代码,如下:
1
2
3
4
5
6
7
8
9
10
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/static/include/taglib.jsp"%>
html>
title>
通知管理
title>
"decorator" "default"/>
"gray-bg">
其中content=“default”中的“default”对应“decorators.xml”配置文件中的“ name”属性值。 通过上面的配置,在加载完的页面,点击右键查看源代码可以看到加载完页面会出现head.jsp页面的代码。
原文链接:https://www.f2er.com/xml/294150.html