strust2异常处理机制
对已Action的处理方法而言,处理结果可能出现如下情况:
1.正常执行,返回一个String类型的逻辑视图
对于String类型的逻辑视图,使用<result name="逻辑视图名">物理视图资源</result>
2.遇到异常,返回一个异常对象
<exception-mapping exception="异常对象" result="逻辑视图名" />
Struts2的异常配置本质:
为不同的异常类,起一个逻辑视图名
struts2的异常配置分为两种:
1.局部异常配置
将<exception-mapping exception="" result="" />作为action的子标签
2.全局异常配置
将<exception-mapping exception="" result="" />作为<global-exception-mappings>子标签
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.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.
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.
struts2用exception-mapping来为每个exception指定专门的Result,<exception-mapping>标签有两个属性,一个为exception,一个为result.前者指定的为指定异常类的full name,result为此异常类的对应的result名称,相当于action方法返回的视图名称(String). 当struts.xml中配有了全局异常和局部异常(针对专门的action配置的异常),struts2显示查找局部异常配置,要是没查到,再查找全局异常配置。查到后,将根据result指定的视图名称来查找Result标签指定的视图资源,同样
它也是先在局部action mapping中查找Result视图资源,要是没有查到,在从<global-result>标签中去查找。
对已Action的处理方法而言,处理结果可能出现如下情况:
1.正常执行,返回一个String类型的逻辑视图
对于String类型的逻辑视图,使用<result name="逻辑视图名">物理视图资源</result>
2.遇到异常,返回一个异常对象
<exception-mapping exception="异常对象" result="逻辑视图名" />
Struts2的异常配置本质:
为不同的异常类,起一个逻辑视图名
struts2的异常配置分为两种:
1.局部异常配置
将<exception-mapping exception="" result="" />作为action的子标签
2.全局异常配置
将<exception-mapping exception="" result="" />作为<global-exception-mappings>子标签
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.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.
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.
struts2用exception-mapping来为每个exception指定专门的Result,<exception-mapping>标签有两个属性,一个为exception,一个为result.前者指定的为指定异常类的full name,result为此异常类的对应的result名称,相当于action方法返回的视图名称(String). 当struts.xml中配有了全局异常和局部异常(针对专门的action配置的异常),struts2显示查找局部异常配置,要是没查到,再查找全局异常配置。查到后,将根据result指定的视图名称来查找Result标签指定的视图资源,同样
它也是先在局部action mapping中查找Result视图资源,要是没有查到,在从<global-result>标签中去查找。
<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>
</struts>
In the example above, here is what happens based upon each Exception:
- A
java.sql.SQLException
will chain to theSQLExceptionAction
(action mapping not shown) - A
com.company.SecurityException
will redirect toLogin.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 |
Sample JSP using Error and Exception Values
<
h2
>An unexpected error has occurred</
h2
>
<
p
>
Please report this error to your system administrator
or appropriate technical support personnel.
Thank you for your cooperation.
</
p
>
<
hr
/>
<
h3
>Error Message</
h3
>
<
s:actionerror
/>
<
p
>
<
s:property
value="%{exception.message}"/>
</
p
>
<
hr
/>
<
h3
>Technical Details</
h3
>
<
p
>
<
s:property
value="%{exceptionStack}"/>
</
p
>
|
Exception in constructors
Global exception mappings are designed to be used with exceptions thrown by action methods (like execute
). exceptions thrown from constructors will not be handled by global exception mappings.