spring集成sitemesh3

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chen_jia_hao/article/details/82711065

1、SiteMesh是什么?

SiteMesh是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的。

SiteMesh是基于Servlet的filter,通过截取response,并进行装饰后再交付给客户。

2、SiteMesh怎么用?

这里使用较新版本SiteMesh3.0(建议使用),和之前版本有点差别。

三步走:

(1)在web.xml中配置 org.sitemesh.config.ConfigurableSiteMeshFilter 过滤器。

(2)创建sitemesh3.xml配置(默认路径:/WEB-INF/sitemesh3.xml),里面可配置哪些路径的页面需要被装饰。

(3)创建SiteMesh模板页面,用于装饰页面。

部分示例代码:

2.1 在web.xml中配置在web.xml中配置 org.sitemesh.config.ConfigurableSiteMeshFilter 过滤器。

 <!--集成sitemesh3 需要配置ConfigurableSiteMeshFilter过滤器-->
  <filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

2.2 在/WEB-INF/下面创建sitemesh3.xml并做相关配置。

<sitemesh>
    <!-- 默认装饰-->
    <mapping decorator="/WEB-INF/view/layouts/default.jsp"/>

    <!--配置特定的装饰路径-->
    <!--<mapping path="/index" decorator="/WEB-INF/view/layouts/default.jsp"></mapping>-->

    <!-- 配置不被装饰的路径 -->
   <!-- <mapping path="" exclue="true"/>-->
    <mapping path="/test" exclue="true"/>

    <!-- 默认情况下,sitemesh 只对 HTTP 响应头中 Content-Type 为 text/html 的类型进行拦截和装饰,
         我们也可以添加更多的 mime 类型 -->
    <mime-type>text/html</mime-type>
</sitemesh>

2.3 创建sitemesh模板页,用于装饰。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>
        <sitemesh:write property='title'/>
    </title>
    <sitemesh:write property='head'/>
</head>
<body>
我是装饰器,title的内容是:<sitemesh:write property='title'/><br/>
我是装饰器,body的内容是:<sitemesh:write property='body'/><br/>
</body>
</html>

到此,三步搞定。

3、参考代码:

下载Springmvc+Sitemesh完整代码demo:https://gitee.com/chen_jia_hao/Sitemesh3-demo.git

demo效果:

4、更多--修改sitemesh3.xml默认是存放的路径

前面说到sitemesh3.xml默认放在/WEB-INF目录下面。那么我放到其他地方可不可呢?当然可以了。我们一起来看下org.sitemesh.config.ConfigurableSiteMeshFilter过滤器的源代码,发现如下:

 public static final String CONFIG_FILE_PARAM = "configFile";
 public static final String CONFIG_FILE_DEFAULT = "/WEB-INF/sitemesh3.xml";

configFile 属性其实就是用于配置sitemesh3.xml的路径。下面那一行就是configFile 的默认值,所以我们可以在配置过滤器的时候,还配置一下 configFile 即可。

猜你喜欢

转载自blog.csdn.net/chen_jia_hao/article/details/82711065