Java SpringMVC 异常处理SimpleMappingExceptionResolver类详解

2022-07-21,,,

spring3.0 对异常的处理方式总共有两种:

一种是使用 handlerexceptionresolver 接口,并且 spring 已经提供默认的实现类 simplemappingexceptionresolver。

第二种方法是在 controller 内部实现,灵活性更高。

从目前的调查结果来看,这两种方式不能共存。我们一般在项目中使用第一种方法。

下面分别描述一下这两种使用方式:

一、基于 handlerexceptionresolver 接口的方式

使用这种方式只需要实现 resolveexception 方法,该方法返回一个 modelandview 对象,在方法内部对异常的类型进行判断,然后返回合适的 modelandview 对象,如果该方法返回了 null,则 spring 会继续寻找其他的实现了 handlerexceptionresolver 接口的 bean。换句话说,spring 会搜索所有注册在其环境中的实现了 handlerexceptionresolver 接口的 bean,逐个执行,直到返回了一个 modelandview 对象。

public class customexceptionhandler implements handlerexceptionresolver {  
  
    @override  
    public modelandview resolveexception(httpservletrequest request,  
            httpservletresponse response, object object, exception exception) {  
        if(exception instanceof ioexception){  
            return new modelandview("ioexp");  
        }else if(exception instanceof sqlexception){  
            return new modelandview("sqlexp");  
        }  
        return null;  
    }  
}

这个类必须声明到 spring 配置文件中,或者使用 @component 标签,让 spring 管理它。同时 spring 也提供默认的实现类 simplemappingexceptionresolver,需要使用时只需要使用注入到 spring 配置文件进行声明即可。自定义实现类与默认的实现类,可同时使用。

示例如下:

<!-- 自定义的实现类 --><bean id="exceptionhandler" class="com.enh.test.customexceptionhandler"/><!-- 默认的实现类注入 --><bean class="org.springframework.web.servlet.handler.simplemappingexceptionresolver">  
    <!-- 为所有的异常定义默认的异常处理页面,exceptionmappings未定义的异常使用本默认配置 -->  
    <property name="defaulterrorview" value="error"></property>  
    <!-- 定义异常处理页面用来获取异常信息的变量名,默认名为exception -->  
    <property name="exceptionattribute" value="ex"></property>  
    <!--     定义需要特殊处理的异常,用类名或完全路径名作为key,异常页文件名作为值,    将不同的异常映射到不同的页面上。   -->  
    <property name="exceptionmappings">  
        <props>  
            <prop key="ioexception">error/ioexp</prop>  
            <prop key="java.sql.sqlexception">error/sqlexp</prop>  
        </props>  
    </property>  
</bean>

一个典型的异常显示界面如下:

<html> 
<head><title>exception!</title></head> 
<body> 
  <% exception ex = (exception)request.getattribute("exception"); %> 
  <h2>exception: <%= ex.getmessage();%></h2> 
  <p/> 
  <% ex.printstacktrace(new java.io.printwriter(out)); %> 
</body> 
</html>

exception 是在 simplemappingexceptionresolver 被存放到 request 中的,具体可以查看源代码。

另外这里配置的异常显示界面均仅包括主文件名,至于文件路径和后缀已经在 viewresolver 中指定。如果找不到页面,会根据错误提示再调整页面路径。

二、controller 内部单独实现

该方法需要定义在 controller 内部,然后创建一个方法并用 @exceptionhandler 注解来修饰用来处理异常,这个方法基本和 @requestmapping 修饰的方法差不多,只是可以多一个类型为 exception 的参数,@exceptionhandler 中可以添加一个或多个异常的类型,如果为空的话则认为可以触发所有的异常类型错误。

@controller  
public class exceptionhandlercontroller {  
      
    @exceptionhandler(value={ioexception.class,sqlexception.class})  
    public string exp(exception ex,httpservletrequest request) {  
        request.setattribute("ex", ex);  
        return "/error.jsp";  
    }  
  
}

三、相关问题

handlerexceptionresolver 和 web.xml 中配置的 error-page 会有冲突吗?

web.xml 中配置 error-page 同样是配置出现错误时显示的页面:

<error-page>
    <error-code>500</error-code>
    <location>/500.jsp</location>
</error-page>

如果 resolveexception 返回了 modelandview,会优先根据返回值中的页面来显示。不过,resolveexception 可以返回 null,此时则展示 web.xml 中的 error-page 的500状态码配置的页面。

api 文档中对返回值的解释:

return a corresponding modelandview to forward to, or null for default processing.

到此这篇关于springmvc 异常处理simplemappingexceptionresolver类详解的文章就介绍到这了,更多相关springmvc 异常处理simplemappingexceptionresolver类内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《Java SpringMVC 异常处理SimpleMappingExceptionResolver类详解.doc》

下载本文的Word格式文档,以方便收藏与打印。