Exception mappings is a powerful feature for dealing with an Action
class that throws an Exception. The core idea is that an Exception
thrown during the Action method can be automatically caught and mapped
to a predefined Result. This declarative strategy is especially useful
for frameworks, like Hibernate and Acegi, that throw RuntimeExceptions.
Exception 映射是一個(gè)強(qiáng)大的功能,用來(lái)處理Action類拋出一個(gè)Exception。核心是一個(gè)Exceptio你在Action期間異常被自動(dòng)抓取,并且映射到預(yù)先定義好的結(jié)果。這個(gè)聲明方式的策略對(duì)framework相當(dāng)有用,例如Hibernate和Acegi,他們拋出RuntimeException。
As with many other parts of the framework, an Interceptor is needed
to activate the exception mapping functionality. Below is a snippet
from struts-default.xml which has the exception mapping already activated.
與框架其他部分類似,一個(gè)Interceptor用來(lái)激活exception mappingfunctionality.下面是struts-default.xml的代碼片斷,在它里面exception映射已經(jīng)被激活。
...
<interceptors>
...
<interceptor name="exception" class="com.opensymphony.xwork.interceptor.ExceptionMappingInterceptor"/>
...
</interceptors>
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servlet-config"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="scoped-model-driven"/>
<interceptor-ref name="model-driven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="static-params"/>
<interceptor-ref name="params"/>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
...
To use exception mapping, we simply need to map Exceptions to
specific Results. The framework provides two ways to declare an
exception mapping <exception-mapping/> - globally or for a specific action mapping. The exception mapping element takes two attributes, exception and result.
用Exception映射,我們簡(jiǎn)單地需要映射Exception到特定的結(jié)果。框架提供兩種方式申明一個(gè)Exception映射<exception-mapping>-全局或特定的Action映射。Exception映射元素使用兩種屬性,exception和result。
When declaring an exception mapping, the Interceptor will find the
closest class inheritance match between the Exception thrown and the
Exception declared. The Interceptor will examine all declared mappings
applicable to the action mapping, first local and then global mappings.
If a match is found, the Result is processed, just as if it had been
returned by the Action.
當(dāng)聲明一個(gè)exception映射,Interceptor將尋找最近的類繼承關(guān)系來(lái)在Exception throw和Exception聲明之間匹配。首先找local,然后找全局。如果找到匹配,結(jié)果將會(huì)被處理,就像它被Action處理返回一樣。
This process follows the same rules as a Result returned from an
Action. It first looks for the Result in the local action mapping, and
if not found, it looks for a global Result.
Below is an example of global and local exception mappings.
<struts>
<package name="default">
...
<global-results>
<result name="login" type="redirect">/Login.action</result>
<result name="Exception">/Exception.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.sql.SQLException" result="SQLException"/>
<exception-mapping exception="java.lang.Exception" result="Exception"/>
</global-exception-mappings>
...
<action name="DataAccess" class="com.company.DataAccess">
<exception-mapping exception="com.company.SecurityException" result="login"/>
<result name="SQLException" type="chain">SQLExceptionAction</result>
<result>/DataAccess.jsp</result>
</action>
...
</package>
</xwork>
In the example above, here is what happens based upon each Exception:
- A java.sql.SQLException will chain to the SQLExceptionAction (action mapping not shown)
- A com.company.SecurityException will redirect to Login.action
- Any other exception that extends java.lang.Exception will return the /Exception.jsp page
Exception Values on the ValueStack
By default, the ExceptionMappingInterceptor adds the following values to the Value Stack:
exception |
The exception object itself |
exceptionStack |
The value from the stack trace |