使用decorator模式对jsp页面进行布局:
步骤:
1.web.xml:
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.新建decorator.xml文件 配置需布局对象与不需要布局对象:
<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/WEB-INF/views/decorators">
<!-- 不需要布局的页面 -->
<excludes>
<pattern>/login</pattern>
<pattern>/uploadImage/**</pattern>
<pattern>/category/preview1/**</pattern>
<pattern>/category/previewNews</pattern>
<pattern>/category/preview/preview/**</pattern>
</excludes>
<!-- 布局的页面 -->
<decorator page="/WEB-INF/views/decorators/decorator.jsp" name="header">
<!-- 对所有符合/category,/pass,/account,/activity 请求路劲的uri jsp页面进行页面布局-->
<pattern>/pass</pattern>
<pattern>/category/**</pattern>
<pattern>/account/**</pattern>
<pattern>/activity/**</pattern>
</decorator>
</decorators>
3.新建decorator.jsp
<?xml version="1.0" encoding="UTF-8" ?>
<%@ include file="../taglibs.jsp"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<!DOCTYPE html>
<!--[if IE 8]>
<html xmlns="http://www.w3.org/1999/xhtml" class="ie8 wp-toolbar" lang="zh-CN">
<![endif]-->
<!--[if !(IE
]><!-->
<html xmlns="http://www.w3.org/1999/xhtml" class="wp-toolbar" lang="zh-CN">
<!--<![endif]-->
<head>
<title><decorator:title /></title>
<decorator:head />
</head>
<body class="wp-admin wp-core-ui js users-php auto-fold admin-bar branch-3-6 version-3-6 admin-color-fresh locale-zh-cn customize-support">
<div id="wpwrap">
<%@ include file="../menu.jsp"%><!--左边菜单栏-->
<div id="wpcontent">
<%@ include file="../nav.jsp"%><!--头部-->
<decorator:body /><!--主体部分-->
<div class="clear"></div>
</div>
</div>
</body>
</html>