`

SiteMesh3简单研究笔记

阅读更多

对应之前的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>

 

 

 

2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics