1

我是这些技术的新手,我正在使用以下框架来开发应用程序:

  • 支柱 2
  • 春天 3

我在 Spring 中实现所有业务逻辑,因此如果发生任何异常,我将向最终用户显示自定义消息。

您能否解释一下如何在这些技术中开发异常处理功能?

4

2 回答 2

1

根据我的理解,最好的方法是为您的业务层定义一些预定义的异常并将这些异常抛出回您的操作类。

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>

这是您的偏好,您希望如何配置它们。有关详细信息,请参阅文档

您甚至可以选择从值堆栈访问异常详细信息。默认情况下,ExceptionMappingInterceptor将以下值添加到值堆栈:

  • exception 异常对象本身
  • exceptionStack 来自堆栈跟踪的值

这是在 JSP 中访问这些对象的方法

 <s:property value="%{exception.message}"/>
 <s:property value="%{exceptionStack}"/>

有关详细信息,请参阅详细信息

于 2012-04-16T06:39:41.760 回答
0

你可以选择那些

支柱 2

春天 3

于 2012-04-16T06:34:14.737 回答