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. 不同异常用一个表示(两个不同异常用一个异常框架处理)

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

strutsconfig.xml中将exception放在全局异常中

strutsconfig.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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值