WebWork2.2に付属されているXWork(1.1)から例外処理用のInterceptorが追加されました。
発生した例外に応じて設定されたResultを実行する機能になっています。
webwork-default.xml
以下の記述が追加されています。
<interceptor name="exception" class="com.opensymphony.xwork.interceptor.ExceptionMappingInterceptor"/>
xwork.xml
package要素の下にglobal-exception-mappings要素を書くか、action要素の下にexception-mapping要素を書いて設定します。global-exception-mappingsはpackage内のAction全体に対する設定で、exception-mappingは特定のactionに対する設定です。
actionにはexceptionインターセプタを設定する必要があります。
<xwork> <include file="webwork-default.xml"/> <package name="default" extends="webwork-default"> <global-results> <result name="error" type="mayaa">error.html</result> <result name="db_error" type="mayaa">db_error.html</result> </global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error"/> <exception-mapping exception="java.sql.SQLException" result="db_error"/> </global-exception-mappings> <action name="exception_test"> <interceptor-ref name="defaultStack"/> <exception-mapping exception="ww2.examples.CustomException" result="custom_error"/> <result name="custom_error" type="mayaa">custom_error.html</result> <result name="success" type="mayaa" /> </action> </package> </xwork>
例外情報の取得
発生した例外の情報はExceptionHolderに格納され、ValueStackに追加されるので、ValueStackから以下の情報が取り出せます。