Spring MVC 异常处理

来源:互联网 发布:淘宝宝贝属性是什么 编辑:程序博客网 时间:2024/05/16 18:44

在Spring mvc中处理异常方式有局部异常和全局异常,局部异常处理方法一般实现在Controller注解的类中。使用throw抛出异常,抛出异常的类添加到handlerException方法注解value上才会被进行处理。

package cn.controller;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.RequestMapping;import cn.exception.UserException;@Controllerpublic class UserController {    @RequestMapping(value="/user")     public String throwException(){        throw new UserException("抛出一个UserException异常!");            }    @ExceptionHandler(value = { UserException.class })//UserException 自己定义的一个异常类 抛出的异常必须声明在value值中,否则无法出处理抛出的异常    public String handlerException(Exception e, HttpServletRequest request) {        request.setAttribute("e", e);//将异常的对象存放到request中        return "error";//跳转到逻辑视图名 在error.jsp页面中用获取异常信息${e.getMessages()}    }}

自定义的异常类继承父类RuntimeException

package cn.exception;public class UserException extends RuntimeException{    private static final long serialVersionUID = 1L;    public UserException() {        super();    }    public UserException(String message, Throwable cause) {        super(message, cause);    }    public UserException(String message) {        super(message);    }    public UserException(Throwable cause) {        super(cause);    }}

定义全局异常需要配置Springmvc的name-servlet.xml文件,将需要处理的异常配置到 SimpleMappingExceptionResolver的props中,抛出的异常会被SimpleMappingExceptionResolver进行处理。在props中每配置prop是对全局异常的声明,抛出的异常是否为prop中的key属性所指的异常类的包名和类名,匹配成功后进行逻辑视图跳转。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd        http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-3.0.xsd        http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">    <!-- 默认注解映射的支持 -->    <mvc:annotation-driven />    <context:annotation-config />    <!-- 使用annotation方式,完成映射 -->    <!-- 让spring扫描包下的所有类,有注解的类生效 -->    <context:component-scan base-package="cn.controller" />    <!--视图解析器 InternalResourceViewResolver 内部资源视图解析器 -->    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/jsp/"></property>        <property name="suffix" value=".jsp"></property>    </bean>    <!-- 全局异常SimpleMappingExceptionResolver -->    <bean        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">        <property name="exceptionMappings">            <props>                <!-- key指类所在包和类名   error是返回逻辑视图名 -->                <prop key="cn.exception.UserException">error</prop>            </props>        </property>    </bean></beans>

简单的异常处理希望能帮助到大家哈!

原创粉丝点击