SiteMesh
来源:互联网 发布:杭州java平均薪资 编辑:程序博客网 时间:2024/06/05 04:44
SiteMesh 是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的
工作原理编辑
SiteMesh是基于Servlet的filter的,即过滤流。它是通过截取response,并进行装饰后再交付给客户。
其中涉及到两个名词: 装饰页面(decorator page)和 “被装饰页面(Content page)” , 即 SiteMesh通过对Content Page的装饰,最终得到页面布局和外观一致的页面,并返回给客户
sitemesh运行环境需要:servlet, JDK 。
装饰页面(decorator page)
<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ taglib prefix="sitemesh" uri="http://www.opensymphony.com/sitemesh/decorator" %><c:set var="ctx" value="${pageContext.request.contextPath}"/><!DOCTYPE html><!--[if IE 8]> <html lang="zh-cn" class="ie8 no-js"> <![endif]--><!--[if IE 9]> <html lang="zh-cn" class="ie9 no-js"> <![endif]--><!--[if !IE]><!--><html lang="zh-cn" class="no-js"><!--<![endif]--><!-- BEGIN HEAD --><head> <meta charset="utf-8"/> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title>xxx<sitemesh:title/></title> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta content="width=device-width, initial-scale=1.0" name="viewport"/> <meta content="" name="description"/> <meta content="" name="author"/> <!-- BEGIN GLOBAL MANDATORY STYLES --></head><!-- END HEAD --><!-- BEGIN BODY --><body class="page-header-fixed "><%@ include file="/WEB-INF/layouts/header.jsp" %><div class="clearfix"></div><!-- BEGIN CONTAINER --><div class="page-container"> <%@ include file="/WEB-INF/layouts/sidebar.jsp" %> <!-- BEGIN CONTENT --> <div class="page-content-wrapper"> <div class="page-content"> <sitemesh:body/> </div> </div> <!-- END CONTENT --></div><%@ include file="/WEB-INF/layouts/footer.jsp" %><sitemesh:getProperty property="page.script"/><!-- END JAVASCRIPTS --></body><!-- END BODY --></html>
被装饰页面(Content page)
<!DOCTYPE html><html><head><meta charset="utf-8"><title>测试页面</title><meta name="author" content="鰟鮍鱼" /><meta name="keywords" content="" /><meta name="description" content="" /></head><body> 111 <div>${username}</div> <div>${rc.contextPath}</div> ${(message)!}</body></html>
classpath目录下decorator.xml
<?xml version="1.0" encoding="utf-8"?><decorators defaultdir="/decorators"> <!-- 此处用来定义不需要过滤的页面 --> <excludes> </excludes> <!-- 用来定义装饰器要过滤的页面 --> <decorator name="main" page="main.jsp"> <pattern>/*</pattern> </decorator></decorators>
web.xml配置 filter
<!-- SiteMesh Web-Page Layout 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>/*</url-pattern> </filter-mapping>
sitemesh 标签
引入标签
<%@ taglib prefix="sitemesh" uri="http://www.opensymphony.com/sitemesh/decorator" %>
我们看一下装饰模板中可以使用的Sitemesh标签
取出被装饰页面的head标签中的内容。
取出被装饰页面的body标签中的内容。
取出被装饰页面的title标签中的内容。default为默认值
取出被装饰页面相关标签的属性值。
decorator.xml命名
配置decorators.xml(必须有),sitemesh.xml(可选,但最好配置)
配置相关的XML文件。在你项目的/WebRoot/WEB-INF/目录下新建一个decorators.xml和一个sitemesh.xml的空的XML文件。在decorators.xml中这样配置:<?xml version="1.0" encoding="UTF-8"?><!--* defaultdir: 包含装饰器页面的目录 * page : 页面文件名 * name : 别名 * role : 角色,用于安全 * webapp : 可以另外指定此文件存放目录 * Patterns : 匹配的路径,可以用*,那些被访问的页面需要被装饰。 --> <decorators defaultdir="/decorators"> <!-- 用来定义不需要过滤的页面 --> <excludes> <!-- 过滤掉load进的页面 --> <pattern>/page/lesquare/specialties/special_java.jsp</pattern> <pattern>/page/lesquare/specialties/special_.net.jsp</pattern> </excludes> <!-- 用来定义装饰器要过滤的页面 --> <decorator name="siteMesh" page="siteMesh.jsp"> <pattern >/*</pattern> </decorator></decorators>在sitemesh.xml可以这样配置:<?xml version="1.0" encoding="UTF-8"?><sitemesh> <property name="decorators-file" value="/WEB-INF/decorators.xml" /> <excludes file="${decorators-file}" /> <page-parsers> <parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" /> <parser content-type="text/html;charset=UTF-8" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" /> </page-parsers> <decorator-mappers> <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper"> <param name="config" value="${decorators-file}" /> </mapper> </decorator-mappers></sitemesh>
开发步骤
1、引入jar包
<!-- https://mvnrepository.com/artifact/opensymphony/sitemesh --> <dependency> <groupId>opensymphony</groupId> <artifactId>sitemesh</artifactId> <version>2.4.2</version> </dependency>
2、配置过滤器 web.xml
<!-- SiteMesh Web-Page Layout 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>/*</url-pattern> </filter-mapping>
3 、配置decorator.xml
4、写装修页
5、写被装修页
0 0
- sitemesh
- SiteMesh
- Sitemesh
- Sitemesh
- SiteMesh
- sitemesh
- sitemesh
- sitemesh
- sitemesh
- sitemesh
- sitemesh
- sitemesh
- SiteMesh
- SiteMesh
- sitemesh
- SiteMesh
- SiteMesh
- Sitemesh
- 用非递归方式实现二叉树先序便利
- 五大常用算法之一:分治算法(转)
- At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger fo
- 【杭电oj2647】Reward
- Leetcode之路 258. Add Digits
- SiteMesh
- eclipse下maven项目开发编译
- 向量vector理解
- HDU 1686 Oulipo
- 五大常用算法之三:贪心算法(转)
- Android:极光推送集成注意要点——解决:Get sdk version fail![获取sdk版本失败!]解决方案
- [编程题]由异或门引出的几道数组的算法题
- TreeSet&NavigableMap&NavigableSet源码解析 给jdk写注释系列之jdk1.6容器(8)
- TensorFlow学习笔记(三):Tutorial第二个例子-深入MNIST-卷积神经网络