有時候在做struts2的返回頁面時,不想讓sitemesh裝飾action所返回的某個特定的頁面,比如:錯誤頁面或者我們自己寫的404頁面。因為sitemesh是根據url來判斷是否裝飾的,如果你提交上去的action已經裝飾了,那么這個action返回回來的result頁面,自然就也會被裝飾,因為此時地址欄的url沒有變化。如果我們此時不想裝飾返回的某個頁面時,可以有以下解決方案:(有關sitemesh配置文件我就不做說明了,網上一大堆,呵呵_..以下是部分配置說明):
struts.xml部分配置
<action name="toIndex" class="indexAction" method="toIndex">
<result name="index">/main.jsp</result>
<result name="error">/error.jsp</result>
</action>
如果我們不想對上面的error.jsp做裝飾,那么我們要在error.jsp頁面的<head>里加一句meta:
<head>
<!-- 不讓sitemesh裝飾此頁面 :content="none"表示在decorators.xml里面的name="none"的裝飾器 -->
<meta name="decorator" content="none"/>
<title>異常頁面</title>
</head>
然后再在decorators.xml中配置一下:
<decorators defaultdir="/layout">
<decorator name="none">
<pattern>/error.jsp*</pattern>
</decorator>
</decorators>
注意:上面的name="none"要和error.jsp頁面中的meta "content"屬性值一樣。好了,配置完了,試試吧,肯定不會再被裝飾了,_。。。
除了上面的解決方案之外,根據網友的提示,貌似在action中設置result的type="redirect"也可以,不過我沒試,有興趣的可以試試,呵呵。。謝謝大家,第一次發稿,請多指教。。