1. 程式人生 > >Struth2框架報錯“警告: Could not find action or result”解決方法

Struth2框架報錯“警告: Could not find action or result”解決方法

Struth2過濾報錯“警告: Could not find action or result”解決方法

用Struth2框架時,如果瀏覽器中輸入了struth.xml中沒有定義的action時,會報以下錯誤:

警告: Could notfind action or result

There is no Action mapped for namespace /and action name dawdwadwaad. - [unknown location]

警告: Could notfind action or result

There is no Action mapped for action namehtml. - [unknown location]

 網上找了一下有以下幾種解決辦法,供參考

第一種方法:

<!-- 這句話意思就是你說的了,只要找不到action,都會找這個預設的action--> <default-action-ref name="defaultAction" /> <!-- 配置全域性result,因為你的action直接返回error,所以我們新增一個全域性的result,用來指示錯誤以後,返回哪個頁面。 --> <global-results> <result name="error" type="redirect">page/error.jsp</result>  //裡面的路徑和JSP檔案你自己寫。 </global-results> <!-- 預設action -->這一步是把你寫的那個action配置到struts中。 <action name="defaultAction" class="前面加你的包名DefaultAction" />  

第二種方法:

類似第一種方法,只是沒用全域性跳轉,而是在DefaultAction中加個execute()方法,直接return "xxxx";

public String execute(){

      return "xxxx";//xxxx自己隨便定義,只要跟struth.xml定義的一樣就行

}

 struth.xml中配置:

<default-action-ref name="defaultAction" /> <!-- 預設action -->這一步是把你寫的那個action配置到struts中。 <action name="defaultAction" class="前面加你的包名 DefaultAction" >   <result name="XXXX">要跳轉的jsp,如login.jsp</result> </action>  

第三種方法:

<struts>         <package name="default" namespace="/" extends="struts-default">         <default-action-ref name="index"></default-action-ref>         <action name="index">                <result>/default.jsp</result>         </action>     </package> </struts>

第四種方法:

不管action還是頁面,如果找不到,都會報404錯誤,那麼在你的專案的web.xml中配置一下,讓所有404錯誤都跳轉到你指定的頁面(比如你定義一個error.jsp頁面),就OK了,配置如下:   <!-- 配置404錯誤時轉向到的頁面 --> <error-page>    <error-code>404</error-code>    <location>/error.jsp</location>   </error-page>