在Struts2应用中,对于未捕获的异常通常有两种处理方式,一是对于所有的Action进行捕获处理,一是对特定Action进行捕获处理。
对所有Action进行捕获
<package name="base" extends="struts-default">
<global-results>
<result name="securityerror">/securityerror.jsp</result>
<result name="error">/jsp/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping
exception="org.apache.struts.register.exceptions.SecurityBreachException"
result="securityerror" />
<exception-mapping exception="java.lang.Exception"
result="error" />
</global-exception-mappings>
</package>
对特定的Action进行捕获
<action name="actionspecificexception" class="org.apache.struts.register.action.Register" method="throwSecurityException">
<exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException"
result="login" />
<result>/register.jsp</result>
<result name="login">/login.jsp</result>
</action>
本文介绍了Struts2框架中两种异常处理方式:全局异常处理和特定Action异常处理。全局异常处理通过配置文件实现,适用于所有Action;特定Action异常处理则是在指定Action中设置异常映射。

被折叠的 条评论
为什么被折叠?



