2013-03-13 77 views
0

我遵循Apache教程,但下面的代碼會出錯。如何在struts2中爲錯誤的動作請求設置默認頁面?

錯誤>>>>

Caused by: org.xml.sax.SAXParseException; systemId:  
file:/C:/Users/target/Project-1.0/WEB- 
INF/classes/struts.xml; lineNumber: 53; columnNumber: 15; The content of element type "package" 
must match "(result-types?,interceptors?,default-interceptor-ref?,default-action-ref?,default- 
class-ref?,global-results?,global-exception-mappings?,action*)". 

請注意,行動和包正確定義爲一次,我把它複製到運行錯誤重定向代碼。 代碼

 <package name="default" namespace="/" extends="struts-default"> 

     <default-action-ref name="UnderConstruction"></default-action-ref> 

      <action name="UnderConstruction"> 
       <result>notFound.jsp</result> 
      </action> 

      <result-types> 
       <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/> 
      </result-types> 

      <action ....> 
      </action> 
       ,,, 
    </package> 

當我改變follwing線

<result>notFound.jsp</result> 

<result type="tiles">notFound.jsp</result> 

的應用程序將運行,但是當我輸入了錯誤的地址,它並不顯示notFound.jsp頁,只是拋出未找到的操作異常。

當我嘗試下面的代碼的應用程序運行,但它並沒有錯請求重定向到underconstruction頁面

<package name="default" namespace="/" extends="struts-default"> 

      <result-types> 
       <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/> 
      </result-types> 

      <action ....> 
      </action> 
       ,,, 
    </package> 

     <package name="Hello" extends="struts-default"> 
      <default-action-ref name="UnderConstruction"></default-action-ref> 

      <action name="UnderConstruction"> 
       <result>notFound.jsp</result> 
      </action> 
     </package> 
+1

的異常狀態明確什麼是錯的。 – 2013-03-13 14:18:45

+0

@DaveNewton是的,但我想一切都是正確的,如問題中所示。我不知道什麼是錯的,我已經添加了以下行以及它運行,但是當我輸入一個錯誤的地址時,它不顯示notFound.jsp page notFound.jsp 2013-03-14 01:33:52

回答

3

你已經證明以上配置是瓷磚整合。你不能簡單地複製粘貼並使其像火箭一樣工作。但也有不同的方法來實現自己的目標,有些計算策略的情況如下:

1日計算策略

創建一個全球性的結果,聲明錯誤或異常

<global-results> 
    <result name="error">/Error.jsp</result> 
    <result name="specific_exception">/Unique.jsp</result> 
    <result name="login" type="redirectAction">Login.jsp</result> 
</global-results> 

和驗證和返回各自的字符串,例如您的基於行爲的入侵行爲出錯或成功。

第二計算策略

可以重定向本身在動作映射

<action name="your_action_name"> 
    <result type="success">Success.jsp</result> 
    <result type="error">notFound.jsp</result> 
</action> 
+0

同意但我是learing S2,並且需要找出造成錯誤的原因,而不僅僅是解決問題。 – 2013-03-14 01:33:10

+0

好....保持它 – 2013-03-14 05:39:11