Struts2异常处理

来源:互联网 发布:查错别字的软件 编辑:程序博客网 时间:2024/06/09 15:18

Struts2异常处理

Struts2提供了强大的异常处理机制,只需要在struts.xml文件中配置异常处理即可,而不需要在Action中捕获异常。

自定义异常类(继承了Exception)

示例如下:

publicclassSecurityException extends Exception {

    private StringerrorMessage;

    public SecurityException() {

        super();

    }

    public SecurityException(Stringmessage) {

        this .errorMessage=message;

    }

    public String getErrorMessage() {

        returnerrorMessage;

    }

}

全局异常

<global-exception-mappings>

        <exception-mappingresult="SQLException"exception="java.sql.SQLException"/>

        <exception-mappingresult="Exception"exception="java.lang.Exception"/>

    </global-exception-mappings>

        <global-results>

            <resultname="SQLException">/error/SQLException.jsp</result>

            <resultname="Exception">/error/Exception.jsp</result>

</global-results>

局部异常

<actionname="hello"class="com.chen.action.Hello">

            <exception-mappingresult="SecurityException"exception="com.chen.exception.SecurityException"/>

            <resultname="success">/sayHao.jsp</result>

            <resultname="SecurityException">/error/SecurityException.jsp</result>

</action>

0 0