使用sitemesh建立複合視圖 – 3.其他討論

來源:互聯網
上載者:User

使用sitemesh建立複合視圖 - 1.hello 

使用sitemesh建立複合視圖 - 2.裝飾器  

使用sitemesh建立複合視圖 - 3.其它討論 <- now

前面的文章已經足以應用sitemesh來改善您的應用,但我發現還有一些其他的東東可能也會對大家有所協助

可列印的介面裝飾
前面說過有1種可列印的裝飾器,可以允許你當用http://localhost/aaa/a.html?printable=true方式訪問時,應用其他的裝飾器(自己指定),給出原始頁面以供列印(免得把header,footer等的花哨的圖片也搭上)。

讓我們來看一看怎樣實現他:

1.首先在WEB-INFO/sitemesh.xml中設定:
  <mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
    <param name="decorator" value="printable" />
    <param name="parameter.name" value="printable" />
    <param name="parameter.value" value="true" />
  </mapper>
這樣就可以通過?printable=true來使用名為printable的裝飾器,而不是用原來的裝飾器。

2.在WEB-INFO/decorators.xml中定義相應的printable裝飾器
  <decorator name="printable" page="printable.jsp"/>

3.最後編寫printable裝飾器/decorators/printable.jsp

<%@ taglib uri="sitemesh-decorator" prefix="decorator" %>
<html>
<head>
  <title><decorator:title /></title>
  <decorator:head />
</head>
<body>

  <h1><decorator:title /></h1>
  <p align="right"><i>(printable version)</i></p>

  <decorator:body />

</body>
</html>

這樣就可以讓一個原始頁面通過?printable=true開關來切換不同的裝飾器頁面。

 

中文問題
由於sitemesh內部所使用的預設字元集為iso-8859-1,直接使用會產生亂碼,我們可以通過以下方法糾正之:

  • 方法1:可以在您所用的application server的設定檔中找一找,有沒有設定encoding或charset的項目,然後設成gbk或gb2312即可
  • 方法2:這也是我們一直使用的方法。
    1.在每一個jsp頁裡設定: <%@ page contentType="text/html; charset=gbk"%> 來告訴server你所要求的字元集。
    2.在每個jsp頁的head中定義:<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=gbk"> 來告訴瀏覽器你所用的字元集。
總結:使用sitemesh最通常的途徑:

1.配置好環境,

2.在WEB-INFO/decroators.xml中描述你將建立的封裝器。

3.開發在decroators.xml中描述的封裝器,最好存放在/_decorators目錄下

4.ok ,可以看看辛勤的成果了

資源:

  • 下載我提供的ant build的例子  
    我在j2sdk-1_4_0,tomcat4.0.3和tomcat5下測試通過,請先運行/build.bat,以產生/dist/web.war檔案,然後實施到你的伺服器即可。
  • opensymphony團隊的項目集合:
    http://sourceforge.net/projects/opensymphony
  • Wiki :http://wiki.opensymphony.com/
  • 在sf的cvs上下載最新版本CVS

關於作者:

陳鵬,西安東軟公司。作為一名狂熱的程式員希望每一天都能成長進步,並希望與大家分享快樂和知識。
請用以下方式和他聯絡:email chen56@msn.com  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.