decorators.xml

  最近維護一個項目,因爲這個項目我沒作開發,所以需要先熟悉項目的框架和業務,昨天被一個問題給整懵了,我不管怎麼看都弄不明白那個鏈接怎麼跳過去的.因爲那個鏈接地址跟實際看到的頁面不一樣.鏈接地址的頁面只是實際看到頁面的一小部分,反反覆覆盯着那個請求名,然後到struts.xml文件中去找實際的跳轉頁面,反覆查了幾遍確定沒問題,但看到的頁面的確跟struts.xml裏面寫的path不一樣. 
   後來在項目中發現有個decorators.xml配置文件,google之,這才明白是它在作怪! 
   最近項目也比較清閒,所以把這個貼出來給大家看看:). 
    要使用decorator標籤需要下載sitemesh.jar包. 
decorator標籤可以輕鬆解決頁面佈局的問題,輕鬆是因爲相比<include>標籤(需要在每個頁面都用他引入JSP)而言,decorator標籤的使用很簡便,只需要在配置文件decorators.xml進行相應的配置再加上一個裝飾器(其實就是一個JSP頁面)即可. 
decorators.xml 
<decorators defaultdir="/decorators"> 
<excludes> 
<pattern>/shared/*</pattern> 
</excludes> 
<decorator name="style" page="style.jsp"> 
<pattern>/*</pattern> 
</decorator> 
</decorators> 

<excludes> 標籤代表不對定義的請求名進行裝飾 
<decorator> 標籤代表對定義的請求名進行相應的裝飾 
現在我門需要定義一個style.jsp文件,官方是稱之爲裝飾器,俺覺得稱他是模版文件更貼切 
style.jsp 

<%@ page contentType="text/html; charset=utf-8"%> 
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %> 
<html> 
  <head> 
    <title><decorator:title default="裝飾器頁面..." /></title> 
    <decorator:head /> 
  </head> 
  <body> 
    <p><font color="red">this is style's header</font></p> 
    <hr> 
    <decorator:body /> 
    <hr> 
    <p><font color="red">this is style's footer</font></p> 
  </body> 
</html> 
需要注意的是style.jsp需要放在 根目錄/decorators目錄下面,當然decorators目錄是需要自己建的. 
在這個JSP裏面,我們要用到decorator標籤了,注意在用之前別忘了定義標籤: 
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %> 

至於<decorator:title>,<decorator:head />,<decorator:body />的作用,後面再說! 
然後要作的就是準備好我們訪問的頁面 
index.jsp: 

<%@ page contentType="text/html; charset=utf-8"%> 
<html> 
<head> 
<title>decorator-index.jsp</title> 
</head> 
<body> 
this is index.jsp. it's a simple page. 
</body> 
</html> 
把這個頁面放在 根目錄下和 根目錄/shared目錄下面一份 
現在把應用運行起來 
訪問: 
http://localhost:8080/testDecorator/index.jsp 
http://localhost:8080/testDecorator/shared/index.jsp 
看看兩個頁面有什麼不一樣的? 
沒錯,第一個鏈接看到的頁面還包含style.jsp的東東. 

而第二個鏈接卻沒有,是因爲在decorators.xml文件在把這類的請求給過濾了. 

現在明白<decorator:title>,<decorator:head />,<decorator:body />的作用了嗎? 
沒錯,他們依次負責輸出用戶所訪問的頁面中title,head,body標籤之間的內容(不包含<title>,<head>,<body>標籤). 
decorator標籤還有一些屬性和用法,在這就不贅述了,大家自己研究吧 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章