sitemesh3-springMVC配置

web开发中,一般的页面布局都有统一的header,和footer,甚至统一侧边栏,只有中间主题部分可能不一样,在每一个页面中引入这些相同的部分不免有些麻烦,sitemesh3提供一种解决方案,通过不同的访问连接匹配,可以是页面布局统一风格。

实际上也就是做了两件事:

1,对布局相同的页面统一风格,只需要通过配置,即可

2,配置不同的布局,通过不同的连接匹配,进行不同的布局

西面就来看一下需要哪些配置:

1,首先加入jar包,可手动下载,maven项目可配置如下

        <dependency>
            <groupId>org.sitemesh</groupId>
            <artifactId>sitemesh</artifactId>
            <version>3.0.0</version>
        </dependency>

2,web.xml中加入过滤器配置(匹配方式根据需要自行设置)

     <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>

3,配置不同布局的装饰页面

 

<sitemesh:write property='head' /><!--引入主体页面中的 head-->
<sitemesh:write property='body' /><!--引入主体页面中的 body-->

一般可能是这样的

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- 引入 css js -->
<sitemesh:write property='head' />
</head>

<body>
<!-- 编写统一风格的header 或者 include -->
<sitemesh:write property='body' />
<!-- 编写统一风格的footer 或者 include -->
</body>
</html>

4,添加sitemesh3配置文件(通过访问连接配置装饰模式)

<sitemesh>
    <mapping path="/*" decorator="/WEB-INF/decorators/content-decorator.jsp" />
    <mapping path="/index.do" decorator="/WEB-INF/decorators/default-decorator.jsp" />
    <mapping path="/login" decorator="/WEB-INF/decorators/login-decorator.jsp" />
    <mapping path="/logout" decorator="/WEB-INF/decorators/login-decorator.jsp" />

</sitemesh>

5,连接访问流程,如访问的是localhost:8080/xxx/index.do spring中直接请求的index.jsp页面

sitemesh中配置的装饰器是default-decorator.jsp,因此index会通过default-decorator.jsp的装饰后展现

 

但是,在使用中发现两个问题:

1,sitemesh通过的是访问连接匹配,如果在controller中将请求转发则还会使用访问时的配置,如访问了/xxx/index.do,但我在这个controller中的词请求转发到/xxx/login.do,那么装饰方式是default-decorator.jsp 而不是login-decorator.jsp

2,在装饰器页面 如default-decorator,和主体jsp如index.jsp 中有可能会产生js问题,具体原因也没有找到,控制台不报错,但有一些不通过sitemesh装饰可执行的js,在sitemesh装饰后无效。

 

posted @ 2015-01-17 23:07  china2k  阅读(3730)  评论(2编辑  收藏  举报