1、手动式异常处理
Manager抛出异常:
throw new RuntimeException("不能删除该机构");
Action捕捉异常:
catch(Exception e) {
ActionMessages msgs = new ActionMessages();
//errors.detail=error{0},e.getMessage()实质等于"不能删除该机构"作为参数传入errors.detail
ActionMessage msg = new ActionMessage("errors.detail",e.getMessage());
msgs.add("detail",msg);
this.saveErrors(request, msgs);
return mapping.findForward("exception");
}
struts-cfg.xml配置全局变量:
<global-forwards>
<forward name="exception" path="/common/exception.jsp"></forward>
</global-forwards>
创建错误显示页面exceptioin.jsp显示异常信息:
<html:errors/>
2、struts自动处理
Manager仍然是抛出异常:
throw new RuntimeException("不能删除该机构");
Action把异常往上层抛:
throws Exception
struts-cfg.xml配置异常标签:
<exception
key="errors.detail"
type="java.lang.RuntimeException"
scope="request"
path="/common/exception.jsp">
</exception>
3、创建一个统一的异常类处理全部的异常,用一个key指向异常的国际化信息:
throw--><exception>-->SystemException-->SystemExceptionHandler
Manager仍然是抛出异常:
throw new SystemException("不能删除该机构","exception.org.del");
struts-cfg.xml配置异常标签:
<global-exceptions>
<exception
key="errors.detail"
type="com.oa.manager.SystemException"
handler="com.oa.manager.SystemExceptionHandler"
scope="request"
path="/common/exception.jsp">
</exception>
</global-exceptions>
SystemException类:
SystemExceptionHandler类:
MessageResources.properties:
exception.org.del=Can't delete the org with suborg