SSM笔记-SpringMVC的异常处理

1、ExceptionHandler注解
作用:根据有额外的异常处理方法,用来定制出错页面的显示效果
步骤:
①Spring配置文件配置mvc:annotation-driven
②创建一个异常处理类,并用@ControllerAdvice修饰该类
③创建一个返回类型为ModelAndView或者自己需要的类型的方法
④用@ExceptionHandler修饰改方法,@ExceptionHandler的参数值为报错类型的类名.class, 如:@ExceptionHandler({ArithmeticException.class})
⑤异常处理类的方法返回值给视图,通过试图显示报错信息

2、注意:
①@ExceptionHandler方法入参可以加入Exception类型的参数,该参数对应发生的异常对象
②@ExceptionHandler方法入场不能传入Map,如果希望异常信息传递到页面上,返回值类型应为ModelAndView
③方法标记异常有优先级,越接近错误类型的会优先调用
④如果handler方法里面找不到@ExceptionHandler修饰的方法来处理异常,则程序会自己去@ControllerAdvice修饰的类中寻找@ExceptionHandler标记的方法来处理异常

3、ResponseStatusExceptionResolver
作用:用于在发生异常时,定制错误状态码和错误状态描述
用法:
①使用@ResponseStatus(reason,value)修饰handler里面的请求方法或者异常处理类的方法
②其中reason是错误描述,value是错误码,错误码的值为HttpStatus下的参数名,如: HttpStatus.FORBIDDEN

4、DefaultHandlerExceptionResolver
作用:对一些SpringMVC的特殊的异常进行处理的类,如:@RequestMapping中因为限制请求类型而导致的异常就由这个异常类处理的

5、SimpleMappingExceptionResolver
作用:可以到Spring配置文件里面配置,并且指定需要跳转的目标页面,并且是带着错误信息跳转的
步骤:只需要到Spring配置文件中配置对应的bean即可
注意:
①配置中exceptionAttribute的value是页面上错误信息的参数名
②配置中exceptionMappings的prop的key是错误类型的全类名
③配置中exceptionMappings的prop标签之间的值为异常时,需要跳转页面的名字

6、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>SpringMVC_13_Exception</display-name>
  <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

7、springmvc.xml

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

    <!-- 配置自动扫描包 -->
    <context:component-scan base-package="com.test.springmvc"></context:component-scan>

    <!-- 配置视图解析器,把handler返回值解析为实际视图 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 配置使用SimpleMappingExceptionResolver -->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionAttribute" value="exception"></property>
        <property name="exceptionMappings">
            <props>
                <prop key="java.lang.ArrayIndexOutOfBoundsException">result</prop>
            </props>
        </property>
    </bean>

</beans>

8、Handler.java

package com.test.springmvc.handlers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class Handler {

    @RequestMapping("/testHandlerExceptionResolver")
    public String testHandlerExceptionResolver(@RequestParam("i") int i){
        System.out.println("testHandlerExceptionResolver");
        System.out.println("result:"+(10/i));
        return "result";
    }

    @RequestMapping("/testSimpleMappingExceptionResolver")
    public String testSimpleMappingExceptionResolver(@RequestParam("i") int i){
        System.out.println("testHandlerExceptionResolver");
        String[] rStrings = new String[2];
        System.out.println(rStrings[i]);
        return "result";
    }

}

9、HandlerException.java

package com.test.springmvc.handlers;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class HandleException {

    //@ResponseStatus(reason="get error", value=HttpStatus.FORBIDDEN)
    @ExceptionHandler({ArithmeticException.class})
    public ModelAndView handlerArithmeticException(Exception e){
        System.out.println("Error Message:  "+e);
        ModelAndView modelAndView = new ModelAndView("result");
        modelAndView.addObject("exception",e);
        return modelAndView;
    }
}

10、index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    <a href="testHandlerExceptionResolver?i=0">testHandlerExceptionResolver</a>
    <br><br>
    <a href="testSimpleMappingExceptionResolver?i=5">testSimpleMappingExceptionResolver</a>
    <br><br>

    ExceptionHandler注解<br>
    作用:根据有额外的异常处理方法,用来定制出错页面的显示效果
    步骤:<br>
    1、Spring配置文件配置mvc:annotation-driven<br>
    2、创建一个异常处理类,并用@ControllerAdvice修饰该类<br>
    3、创建一个返回类型为ModelAndView或者自己需要的类型的方法<br>
    4、用@ExceptionHandler修饰改方法,@ExceptionHandler的参数值为报错类型的类名.class, 如:@ExceptionHandler({ArithmeticException.class})<br>
    5、异常处理类的方法返回值给视图,通过试图显示报错信息<br>

    注意:<br>
    1、@ExceptionHandler方法入参可以加入Exception类型的参数,该参数对应发生的异常对象<br>
    2、@ExceptionHandler方法入场不能传入Map,如果希望异常信息传递到页面上,返回值类型应为ModelAndView<br>
    3、方法标记异常有优先级,越接近错误类型的会优先调用<br>
    4、如果handler方法里面找不到@ExceptionHandler修饰的方法来处理异常,则程序会自己去@ControllerAdvice修饰的类中寻找@ExceptionHandler标记的方法来处理异常<br>

    <br>
    <br>
    ResponseStatusExceptionResolver<br>
    作用:用于在发生异常时,定制错误状态码和错误状态描述<br>
    用法:<br>
    1、使用@ResponseStatus(reason,value)修饰handler里面的请求方法或者异常处理类的方法<br>
    2、其中reason是错误描述,value是错误码,错误码的值为HttpStatus下的参数名,如: HttpStatus.FORBIDDEN<br>

    <br>
    <br>
    DefaultHandlerExceptionResolver<br>
    作用:对一些SpringMVC的特殊的异常进行处理的类,如:@RequestMapping中因为限制请求类型而导致的异常就由这个异常类处理的<br>

    <br>
    <br>
    SimpleMappingExceptionResolver<br>
    作用:可以到Spring配置文件里面配置,并且指定需要跳转的目标页面,并且是带着错误信息跳转的<br>
    步骤:只需要到Spring配置文件中配置对应的bean即可<br>
    注意:<br>
    1、配置中exceptionAttribute的value是页面上错误信息的参数名<br>
    2、配置中exceptionMappings的prop的key是错误类型的全类名<br>
    3、配置中exceptionMappings的prop标签之间的值为异常时,需要跳转页面的名字<br>
</body>
</html>

11、result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    exception >>>>>>>> ${exception }

</body>
</html>

12、项目目录
项目目录

13、demo
https://download.csdn.net/download/qq_22778717/10601274

猜你喜欢

转载自blog.csdn.net/qq_22778717/article/details/81634220
今日推荐