简介:
sitemesh应用Decorator模式,用filter截取request和response,把页面组件head,content,banner结合为一个完整的视图。通常我们都是用include标签在每个jsp页面中来不断的包含各种header, stylesheet, scripts and footer,现在,在sitemesh的帮助下,我们可以开心的删掉他们了。如下图,你想轻松的达到复合视图模式,那末看完本文吧。
一、在WEB-INF/web.xml中copy以下filter的定义:
<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
二、copy所需sitemesh-2.3.jar到WEB-INF\lib下。(这里可以下载http://www.opensymphony.com/sitemesh/)
三、 建立WEB-INF/decorators.xml描述各装饰器页面。
<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/WEB-INF/layouts/"><!-- 装饰器的路径 -->
<excludes><!-- 不需要做装饰的页面 -->
<pattern>/static/*</pattern>
<pattern>/admin/finance/order_detail</pattern>
<pattern>/admin/jd/*</pattern>
</excludes>
<!-- 装饰器 -->
<decorator name="home" page="/WEB-INF/views/home/index.jsp">
<pattern>/*</pattern><!-- 装饰哪些页面(所有页面) -->
</decorator>
<decorator name="msg" page="/WEB-INF/layouts/msg.jsp">
<pattern>/*</pattern>
</decorator>
<decorator name="default" page="/WEB-INF/layouts/default.jsp">
<pattern>/*</pattern>
</decorator>
</decorators>
四、装饰器页面示例:
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="sitemesh" uri="http://www.opensymphony.com/sitemesh/decorator"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="contextPath" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html>
<head>
<title>xxx</title>
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="${contextPath}/static/css/bootstrap.min.css?ver=${sessionScope.managerVer}">
<link rel="stylesheet" href="${contextPath}/static/css/bootstrap-multiselect.css" >
<link rel="stylesheet" type="text/css" href="/static/css/bootstrap-union-theme.css" />
<link rel="stylesheet" type="text/css" href="${contextPath}/static/css/style.css?ver=${sessionScope.managerVer}" />
<link rel="stylesheet" type="text/css" href="${contextPath}/static/css/union.css?ver=${sessionScope.managerVer}" />
<script type="text/javascript" src="${contextPath}/static/js/prompt.js"></script>
<link rel="stylesheet" type="text/css" href="${contextPath}/static/css/prompt.css?ver=${sessionScope.managerVer}" />
<script type="text/javascript" src="${contextPath}/static/js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="${contextPath}/static/js/jquery.validate.min.js"></script>
<script type="text/javascript" src="${contextPath}/static/js/jquery.form.js"></script>
<script type="text/javascript" src="${contextPath}/static/js/highcharts/highcharts.js"></script>
<script type="text/javascript" src="${contextPath}/static/js/highcharts/modules/exporting.js"></script>
<script type="text/javascript" src="${contextPath}/static/js/ie6_png.js"></script>
<script type="text/javascript" src="${contextPath}/static/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="${contextPath}/static/js/template/template.js"></script>
<!--[if lt IE 9]>
<script type="text/javascript" src="${contextPath}/static/js/html5shiv.min.js"></script>
<![endif]-->
<script type="text/javascript" src="${contextPath}/static/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${contextPath}/static/js/bootstrap-multiselect.js"></script>
<script type="text/javascript" src="${contextPath}/static/js/parsley.js"></script>
<script type="text/javascript" src="${contextPath}/static/js/parsley.remote.js"></script>
<script type="text/javascript" src="${contextPath}/static/js/union.js?ver=${sessionScope.managerVer}"></script>
<script type="text/javascript" src="${contextPath}/static/js/openWin.js?ver=${sessionScope.managerVer}"></script>
<sitemesh:head />
</head>
<body>
<div class="body-all">
<!-- 顶部 -->
<jsp:include page="../views/common/header.jsp"></jsp:include>
<!-- 中部 -->
<div class="center clearfix">
<div class="left">
<!-- 菜单列表 -->
<jsp:include page="../views/common/menu.jsp"></jsp:include>
</div>
<div class="right">
<sitemesh:body />
</div>
</div>
</div>
<!-- 底部-->
<jsp:include page="../views/common/footer.jsp"></jsp:include>
</body>
</html>
decorators.xml的用法
猜你喜欢
转载自779105100.iteye.com/blog/2282135
今日推荐
周排行