struts2 之 异常处理

1.在执行action处理的时候,很多情况下都会有异常抛出。但是,不能直接将异常信息给用户显示。

2.Action异常处理步骤:

  a) 在处理方法抛出异常:

public class HelloAction {
    
    public String hello()throws MyException{
        System.out.println("执行hello");
        if(1==1)
            throw new MyException("系统发生异常");
        return Action.SUCCESS;
    }
}

b)配置发生异常后应该到什么页面:

<action name="hello" class="cn.sxt.action.HelloAction" method="hello">
            <result>/index.jsp</result>
            <result name="myexe">/error.jsp</result>
            <!-- result定义异常发生后 去哪个页面  exception填写异常的完全限定名 -->
            <exception-mapping result="myexe" exception="cn.sxt.exception.MyException"></exception-mapping>
        </action>
原文地址:https://www.cnblogs.com/forever2h/p/6734288.html