`
kaixuan489577823
  • 浏览: 31394 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

SSH中异常处理

    博客分类:
  • ssh
阅读更多
SSH中异常处理
异常处理

在实际项目开发中异常是需要统一处理的,

截获OrgManagerImpl中抛出的信息,要不要都行

利用struts中的处理机制(1.编程处理(手工):try/catch截获异常(这种在统一的时候不用)   2.将异常抛出交给struts自动处理)

1.       手工处理(编程处理)

在action中

//异常处理

    try {

        orgManager.delOrg(id);

    } catch (Exception e) {

        //1. 编程式处理 return mapping.findForward("del_fail");

        //2. 手工处理

        ActionMessages msgs=new ActionMessages();

        ActionMessage msg=new ActionMessage("errors.detail",e.getMessage());

        msgs.add("detail",msg);

        this.saveErrors(request, msgs);

        return mapping.findForward("exception");

<forward name="exception" path="/org/exception.jsp"></forward> 


2.       Struts中自动处理

Action

<action 

           path="/org"

           type="org.springframework.web.struts.DelegatingActionProxy"

异常类型
 
MessageResources.properties中定义的错误类型
 
           name="orgForm"

           scope="request"

           parameter="method"

       >

           <exception key="errors.detail" type="java.lang.RuntimeException" scope="request" path="/org/exception.jsp"></exception>这句就自动处理

           <forward name="index" path="/org/index.jsp"></forward>

           <forward name="add_input" path="/org/add_input.jsp"></forward>

       </action>

       此种自动异常处理缺点:如果异常带很多参数,那么自动异常是没办法处理多参数的

3.  不同异常用一个表示(两个不同异常用一个异常框架处理)

定义统一的异常,通常通过构造器来传递参数。

在struts—config.xml中将exception放在全局异常中

struts—config.xml

com.ygp.oa.manager中创建SystemException类,继承RuntimeException类,并通过复写父类建立相关构造器。

(此时throw new SystemException("存在子机构,不允许删除","exception.org.del");可以在MessageResources.properties中定义相关异常)



默认情况下使用struts中异常处理器handler,

<global-exceptions>

       <exception key="errors.detials" 

                 type="com.ygp.oa.manager.SystemException"

                 path="/common/exception.jsp"

                 scope="request"

                 handler="com.ygp.oa.web.actions.SystemExceptionHandler"

                 >

       </exception>

</global-exceptions>

package com.ygp.oa.web.actions;中

public class SystemExceptionHandler extends ExceptionHandler { 

    public ActionForward execute(

           Exception ex, //后台传递过来的exception通过orgaction throw抛出,传来exception

           ExceptionConfig ae,//代表exception中的所有配置

           ActionMapping mapping, 

           ActionForm formInstance,

           HttpServletRequest request, 

           HttpServletResponse response)

           throws ServletException {

       return super.execute(ex, ae, mapping, formInstance, request, response);

     }  

}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/liusha0007/archive/2009/01/20/3840591.aspx
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics