2011-08-02 17 views
2

私はstruts 2を使ってウェブサイトを構築しています。 はここに私の "struts.xml" のクリップである:Struts 2ワイルドカード構成を使用すると404エラーを返す方法はありますか?

<action name="*" class="com.domain.actions.UserAction" method="{1}">   
    <result name="myresult">/Pages/myresult.jsp</result> 
    <!-- there are many other results --> 
</action> 

は今、私は問題を抱えて。
"aaabbb"のように私が設計しなかったアクションを訪れたとき、サーバーは500エラーを返します。
ワイルドカード構成のため、struts 2は "com.domain.actions.UserAction"クラスの "aaabbb"メソッドを呼び出そうとしますが、 "aaabbb"メソッドは実行されません。
しかし、論理的には、404エラーがより良いです。
これらの状況で404エラーを返し、同時にワイルドカード構成を使用するにはどうすればよいですか?

答えて

4

私はStruts2のの専門家だが、あなたはこれを試みる場合があります:

<global-exception-mappings> 
    <exception-mapping exception="java.lang.Exception" result="error"/> 
</global-exception-mappings> 

次にメソッド属性値がない場合のStruts 2では404

2

を返す「エラー」のアクションを持っていますexecute()メソッドが呼び出されます。

この

はあなたがそうのようなあなたのexecuteメソッドでカスタム結果の値を返すことができることを意味します

public String execute() throws Exception { 
    // if the method * does not exist, we will return a "404ERROR" result 
    return "404ERROR"; 
} 

は、その後、あなたのstruts.xmlマッピングで、あなたがタイプ「httpheader」などである結果の型を作成することができますso:

<action name="*" class="com.domain.actions.UserAction" method="{1}">   
    <result name="myresult">/Pages/myresult.jsp</result> 
    <!-- there are many other results --> 
    <result name="404ERROR" type="httpheader"> 
     <param name="error">404</param> 
     <param name="errorMessage">This is a 404 error. Method does not exist</param> 
    </result> 
</action> 

これはヘッダーで404を呼び出します。これは、あなたがそうのようなあなたのweb.xmlに持ってどんなエラーマッピングにユーザーがかかります。

<error-page> 
    <error-code>404</error-code> 
    <location>/error404.html</location> 
</error-page> 
0

404(それは私の場合で働いていただけだ)

public String execute() {    
    ServletActionContext.getResponse().setStatus(HttpServletResponse.SC_NOT_FOUND); 
    return SUCCESS; 
} 
を返します。もう一つのアイデアを
関連する問題