对应之前的SiteMesh2简单研究笔记 http://sgq0085.iteye.com/blog/2072882
一.导入依赖
导入在中央库中的依赖方法如下
<dependency> <groupId>org.sitemesh</groupId> <artifactId>sitemesh</artifactId> <version>3.0.0</version> </dependency>
二.web.xml中添加Filter
在web.xml中添加下面这个filter
<filter> <filter-name>sitemesh</filter-name> <!--<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>--> <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class> </filter> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
三.装饰器decorator
编写装饰器JSP页面 WEB-INF/decorator/decorator.jsp
<!DOCTYPE html> <html> <head> <title>My Site - <sitemesh:write property='title'/></title> <sitemesh:write property='head'/> </head> <body> <sitemesh:write property='body'/> </body> </html>
四.配置文件sitemesh3.xml
<sitemesh> <!--默认情况下,sitemesh 只对 HTTP 响应头中 Content-Type 为 text/html 的类型进行拦截和装饰,可以添加更多的 mime 类型--> <mime-type>text/html</mime-type> <mime-type>application/xhtml+xml</mime-type> <!-- 不被sitemesh装饰的路径 --> <mapping path="/static/*" exclue="true"/> <mapping path="/api/*" exclue="true"/> <!-- 全局默认装饰器 --> <mapping path="/*" decorator="/WEB-INF/decorator/decorator.jsp"/> <!-- 同时使用多个装饰器 --> <mapping path="/site*"> <decorator>/WEB-INF/decorator/decorator.jsp</decorator> <decorator>/WEB-INF/decorator/decorator2.jsp</decorator> </mapping> <!-- 多个地址使用同一个装饰器 --> <mapping path="*.htm" decorator="/WEB-INF/decorator/html.jsp"/> <mapping path="*.html" decorator="/WEB-INF/decorator/html.jsp"/> </sitemesh>
五.与Spring MVC结合时灵活运用方式
当与Spring MVC结合使用的时候,如果需要针对不同的页面,展示不同的目录功能,其他的页面都可以通过路径来很容易的实现,但是由于首页一般是通过下面这段代码配置
<mvc:view-controller path="/" view-name="portal" />
有两种方法解决
第一种,将上面一段配置改为如下
<mvc:view-controller path="/" view-name="redirect:/portal" /> <mvc:view-controller path="/portal" view-name="portal" />
第二种方法,写一个Controller,RequestMapping匹配路径/portal,由Controller返回portal,通过这种方式,可以在Controller方法中完成一些特殊的要求。比如某些情况下不是到首页而是其他路径。
<mvc:view-controller path="/" view-name="redirect:/portal" />
写在后面
1.使用sitemesh统一页眉页脚菜单等,针对另外一些引用比如引入jqGrid需要引入一些CSS和JS文件,还是统一到一个JSP中,再在具体的JSP页面中通过指令元素include引入
<%@ include file="/WEB-INF/****/****.jsp"%>
2.引入的时候需要注意 page指令中contentType属性如果各个JSP不一致(比如text/html;和charset=UTF-8中间有没有空格)会导致异常。
3.放一个自己的decorator.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:set var="ctx" value="${pageContext.request.contextPath}"/> <%--使用sitemesh统一页眉页脚菜单等,具体JSP中使用JSP指令元素include引入具体组件组装的JSP--%> <%--<%@ include file="/WEB-INF/****/****.jsp"%>--%> <%--添加标准模式(standard mode)的声明 CSS1Compat 而不是BackCompat --%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%--语言属性 根元素指定 lang 属性--%> <html lang="zh-CN"> <head> <title>WEB <sitemesh:write property='title'/></title> <%--字符编码 明确声明字符编码--%> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <meta http-equiv="Cache-Control" content="no-store"/> <meta http-equiv="Pragma" content="no-cache"/> <meta http-equiv="Expires" content="0"/> <%--IE兼容模式 通知IE采用其所支持的最新的模式--%> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <%--国产浏览器默认采用高速模式webkit内核--%> <meta name="renderer" content="webkit"> <link type="image/x-icon" rel="shortcut icon" href="${ctx}/static/images/favicon.ico"> <%--HTML5中引入css文件的标签link的type属性默认值为"text/css",所以不需要设置--%> <link rel="stylesheet" type="text/css" href="${ctx}/static/bootstrap/3.2.0/css/bootstrap.min.css"/> <!--[if lt IE 9]> <script type="text/javascript" src="${ctx}/static/html5css3/html5shiv.min.js"></script> <script type="text/javascript" src="${ctx}/static/html5css3/respond.min.js"></script> <![endif]--> <%--HTML5中引入JS文件的标签lscripttype属性默认值为"text/javascript",所以不需要设置--%> <script type="text/javascript" src="${ctx}/static/jquery/jquery-1.11.1/jquery-1.11.1.min.js"></script> <script type="text/javascript" src="${ctx}/static/bootstrap/3.2.0/js/bootstrap.min.js"></script> <sitemesh:write property='head'/> </head> <body> <sitemesh:write property='body'/> </body> </html>
相关推荐
Sitemesh 3 的使用及配置
sitemesh3-demo 配置说明参见http://blog.csdn.net/thc1987/article/details/6913416
sitemesh3包括lib,api,demo,配置说明,希望大家喜欢
SiteMesh入门学习,很好的学习总结,让你在短时间里掌握sitemesh
很简单的sitemesh入门教程,希望对大家有所帮助
springmvc + mybatis + sitemesh3 超简洁例子 整合springmvc mybatis 方法请看: http://blog.csdn.net/kokoyuo/article/details/52808510
sitemesh入门demo。博客 Sitemesh入门和使用笔记 对应源码
SpringMVC-Freemarker_Sitemesh3例子, maven构建。
springmvc + mybatis + sitemesh3登陆 超简洁例子mysql 增加了MD5验证
sitemesh 装饰 母版
web布局框架 SiteMesh教程及SiteMesh官方文档翻译
siteMesh demo siteMesh使用文档
sitemesh3的sdk包含demo,帮助文档,包等
sitemesh技术的应用.doc
sitemesh 应用 Decorator 模式,用 filter 截取 request 和 response,把页面组件 d,content,banner 结合为一个完整的视图。通常我们都是用 include 标签在每个 jsp 页面中来 断的包含各种header , ...
简单的sitemesh maven项目。亲测可用
SiteMesh官方文档翻译(含Freemarker但不包括velocity部分)
sitemesh.jar包 sitemesh.jar 包sitemesh.jar 包sitemesh.jar包
sitemesh3的一个demo,附带配置说明
spring-boot-sitemesh 源码整合