sitemesh網頁布局


看項目時發現對應頁面下找不到側欄部分代碼,仔細觀察后發現頁面引入了sitemesh標簽,查了下資料原來是頁面用了sitemesh框架解!耦!了!

以前多個模塊包含相同模塊時總是include jsp文件,沒感覺多么麻煩,但看了sitemesh,感覺可以非常簡單!

sitemesh通過基於ServletFilter截取request和response,並給原始的頁面介入一定的裝飾,然后把結果返回給客戶端,被裝飾的頁面並不知道sitemesh的裝飾。

使用步驟如下:(sitemesh運行環境需要:servlet, JDK)

1,引入maven依賴

<dependency>
            <groupId>opensymphony</groupId>
            <artifactId>sitemesh</artifactId>
            <version>2.4.2</version>
</dependency>

2,web.xml中添加過濾器:

<filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <servlet-name>springmvcServlet</servlet-name>
    </filter-mapping>

<servlet>
        <servlet-name>springmvcServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>***</param-name>
            <param-value>***springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvcServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

3.在WEB-INF目錄下添加sitemesh配置文件decorators.xml

<?xml version="1.0" encoding="utf-8"?>
<decorators defaultdir="/WEB-INF/layout">
    <!-- 此處用來定義不需要過濾的頁面 -->
    <excludes>
        <pattern>/login</pattern>

   <pattern>/static/*</pattern> 
       <!--…… -->     
    </excludes>

    <!-- 定義用來裝飾的頁面 -->
    <decorator name="default" page="yanan7890.jsp">
        <pattern>/*</pattern>
    </decorator>
</decorators>

4.定義yanan7890.jsp頁面,根據放在decorators defaultdir配置,放在/WEB-INF/layout/目錄下

<%@page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="sitemesh" uri="http://www.opensymphony.com/sitemesh/decorator"%>
<!DOCTYPE html>
<html>
<head>

<title>SiteMesh示例-<sitemesh:title/></title>  <!-- 會自動替換為被過濾頁面的title.sitemesh:title可選-->

<!--也可以引入需要復用的css和js-->

<link href="/***/.css" rel="stylesheet" type="text/css">
<script src="/***/.js"></script>
<sitemesh:head/><!--會把被過濾頁面head里面的東西(除了title)放在這個地方-->
</head>
<body>

<%@ include file="/common/head.jsp"%>  

<div>
            <sitemesh:body/><!--被過濾的頁面body里面的內容放在這里。-->
</div>
    <%@ include file="/common/foot.jsp"%>
</body>
</html>

如步驟3配置,如訪問/login和/static下的頁面不會裝飾,訪問其它頁面會按照yanan7890.jsp攔截裝飾

至此,大功告成!


參考文章:

1.http://cuisuqiang.iteye.com/blog/2066166

2.http://www.cnblogs.com/shanshouchen/archive/2012/08/10/2631819.html

3.http://baike.baidu.com/item/sitemesh


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM