日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

sitemesh的使用

發布時間:2025/5/22 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sitemesh的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

由于最近項目的原因,接觸到了sitemesh。

SiteMesh是一個用來在JSP中實現頁面布局和裝飾(layout and decoration)的框架組件,能夠幫助網站開發人員較容易實現頁面中動態內容和靜態裝飾外觀的分離。提供了一種在網站中更有效的組織頁面布局的方式。

???? SiteMesh設計思想是,用戶發送request至服務器,服務器根據此request生成動態數據,生成網頁,準備返回給客戶端。就在返回前,SiteMesh進行攔截,對此網頁進行解析,將title、body等部分拆解出來,套上模板后,再返回給客戶端。由于SiteMesh在返回客戶端的最后一步工作,此時的網頁已經具備了標準的html網頁格式,因此SiteMesh只需解析標準的html網頁,無需考慮各個Web應用是應用了JSP、ASP,還是Velocity技術,相當靈活。

???? SiteMesh使用了Decorator的設計模式。

下面我就用一個通俗易懂的例子告訴大家怎么使用它。


這個就是我這個項目的結構,jsp頁面都可以隨意放只要指明路徑就可以了,decorators.xml放在WEB-INF下面。
首先是decorators.xml的書寫:

[html]?view plaincopy print?
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <decorators?defaultdir="/decorators">??
  • ????<!--?用第一套模板?-->??
  • ????<decorator?name="basic-theme"?page="basic-theme.jsp">??
  • ????????<pattern>/data/*</pattern>??
  • ????</decorator>??
  • ????<!--?用第二套模板?-->??
  • ????<decorator?name="basic-theme2"?page="basic-theme2.jsp">??
  • ????????<pattern>/data2/*</pattern>??
  • ????</decorator>??
  • ????<!--?不做任何裝飾?-->??
  • ????<decorator?name="none">??
  • ????????<pattern>/data3/*</pattern>??
  • ????</decorator>??
  • </decorators>??


  • 你可以選擇你自己的模板在page里面定義,<pattern>里面的是你要作用的頁面的路徑。
    下面是aa.jsp

    [html]?view plaincopy print?
  • <%@?page?language="java"?import="java.util.*"?pageEncoding="utf-8"%>??
  • <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">??
  • <html>??
  • ??<head>??
  • ????<title>jsp</title>??
  • ??</head>??
  • ??<body>??
  • ????我愛中國!??
  • ??</body>??
  • </html>??

  • aa2.jsp

    [html]?view plaincopy print?
  • <%@?page?language="java"?import="java.util.*"?pageEncoding="utf-8"%>??
  • <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">??
  • <html>??
  • ??<head>??
  • ????<title>jsp</title>??
  • ??</head>??
  • ??<body>??
  • ????我愛中國2!??
  • ??</body>??
  • </html>??

  • aa3.jsp

    [html]?view plaincopy print?
  • <%@?page?language="java"?import="java.util.*"?pageEncoding="utf-8"%>??
  • <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">??
  • <html>??
  • ??<head>??
  • ????<title>jsp</title>??
  • ??</head>??
  • ??<body>??
  • ????我愛中國3!??
  • ??</body>??
  • </html>??

  • 最重要的xml的配置:

    加上下面一句

    [html]?view plaincopy print?
  • <!--?過濾作用,使發送給客戶端的頁面通過decorator裝飾再返回?-->??
  • ?<filter>??
  • ????<filter-name>sitemesh</filter-name>??
  • ????<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>??
  • ?</filter>??
  • ?<filter-mapping>??
  • ????<filter-name>sitemesh</filter-name>??
  • ????<url-pattern>/*</url-pattern>??
  • ?</filter-mapping>??

  • 下面是裝飾頁面basic-theme.jsp


    basic-theme2.jsp


    下面來看看效果圖:
    下面是用了第一個裝飾模板的頁面圖。


    下面是用了第二個裝飾模板的頁面。



    下面是沒有用裝飾模板的頁面。


    看到這里相信大家應該對sitemesh很期待了吧,趕緊自己去試試吧。這是jar包下載地址。

    sitemesh.jar

    總結

    以上是生活随笔為你收集整理的sitemesh的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。