[英]Exception Handling in Struts2 with Spring3
我是这些技术的新手,并且正在使用以下框架来开发应用程序:
我正在Spring中实现所有业务逻辑,因此万一发生任何异常,我将向最终用户显示自定义消息。
您能否解释一下如何在这些技术中开发异常处理功能?
根据我的理解,最好的方法是为您的业务层定义一些预定义的异常,并将这些异常返回给您的Action类。
S2提供了多种方法来处理这些异常并将其显示给用户,以下是其中的几种
使用Struts 2框架,您可以在struts.xml中指定框架应如何处理未捕获的异常。 处理逻辑可以应用于所有动作(全局异常处理)或特定动作。 让我们首先讨论如何启用全局异常处理。
<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>
<global-results>
<result name="securityerror">/securityerror.jsp</result>
<result name="error">/error.jsp</result>
</global-results>
即使您希望进行精细的级别控制,您也可以根据每个操作随意配置异常处理
<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>
您可以根据自己的喜好选择配置方式,有关详细信息,请参阅文档
您甚至可以选择从Value-Stack访问Exception详细信息。默认情况下, ExceptionMappingInterceptor
将以下值添加到Value Stack中:
这是在JSP中访问那些对象的方法
<s:property value="%{exception.message}"/>
<s:property value="%{exceptionStack}"/>
有关详细信息,请参阅详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.