溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

sitemesh 使用整理(入門)

發(fā)布時(shí)間:2020-06-14 04:53:22 來源:網(wǎng)絡(luò) 閱讀:1128 作者:xiangZander 欄目:開發(fā)技術(shù)

sitemesh是jsp頁(yè)面的一個(gè)前端框架,其主要思想是GOF設(shè)計(jì)模式中的裝飾器模式,在筆者看來就是提高代碼的重用性,減少重復(fù)的代碼,方面工程的管理。具體的還不清楚,寫下這博文知識(shí)為了鞏固和記錄自己今天使用sitemesh的一些筆記。

使用sitemesh的步驟:

  1. 導(dǎo)入 sitemesh的jar包,該包可以在官網(wǎng)上下載最新的穩(wěn)定版。目前最新是sitemesh-2.4.2.jar

  2. 配置sitemesh的核心過濾器,主要用來攔截需要被裝飾的頁(yè)面。

  3. 在工程的WEB-INF目錄下面創(chuàng)建一個(gè)decorators.xml文件,里面主要使用來聲明需要被攔截裝飾的頁(yè)面和不需要攔截的頁(yè)面。

  4. 之后就可以創(chuàng)建具體的頁(yè)面來進(jìn)行測(cè)試了,要使用裝飾器的頁(yè)面需要寫meta屬性來說明。具體內(nèi)容看以下代碼。


(1)web.xml中需要加入sitemesh的過濾器

<filter>
      <filter-name>sitemesh</filter-name>
      <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>sitemesh</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>


(2)decorators.xml文件

<decorators defaultdir="/layouts">
    
    <!-- 不需要過濾的請(qǐng)求 -->
    <excludes>
        <pattern>/static/*</pattern>  <!-- 表示在static文件夾下的所有頁(yè)面都不需要進(jìn)行裝飾 -->
    </excludes>

    <!-- 定義裝飾器要過濾的頁(yè)面 -->
    <decorator name="default" page="default.jsp">     <!-- 表示對(duì)裝飾器頁(yè)面的聲明 -->
        <pattern>/*</pattern>
    </decorator>
</decorators>

(3)default.jsp裝飾器頁(yè)面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="sitemesh" uri="http://www.opensymphony.com/sitemesh/decorator"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<!-- 被裝飾頁(yè)面head部分內(nèi)容將會(huì)被放進(jìn)這里  -->

<title>SiteMesh 示例-<sitemesh:title/></title>    
<sitemesh:head/>            <!-- 被裝飾頁(yè)面head部分內(nèi)容將會(huì)被放進(jìn)這里  -->

</head>
<body>
    <h4>我是裝飾器,我在被裝飾頁(yè)面的body內(nèi)容之前</h4>
    <div id="content">
        <sitemesh:body/>    <!-- 被裝飾頁(yè)面body內(nèi)容將會(huì)被放進(jìn)這里  -->
    </div>
    <h4>我是裝飾器,對(duì)頁(yè)面進(jìn)行裝飾</h4>
</body>
</html>

(4)index.jsp測(cè)試頁(yè)面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>我是index.jsp的title</title>
</head>
<body>
    <h4>我是index的body</h4>
</body>
</html>

(5)執(zhí)行畫面

該畫面在附件

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI