- @ExceptionHandler : 该方法表示在方法上,表示该方法专门用来处理异常信息
- @ResponseStatus:自定义异常,标识在方法上
- SimpleMappingExceptionResolver 配置方式
@ExceptionHandler 案例
项目架构
MyController类
@Controller
public class MyController
{
@RequestMapping(value = "/exhandle1")
public String handle1(@RequestParam("i") Integer i){
System.out.println(10/i);
return "success";
}
}
MyExceptionContext (异常信息处理类)
// ControllerAdvice表明这个是一个专门的异常处理类
@ControllerAdvice // 这个类只能使用ControllerAdvice注解 不能 使用Controller注解
public class MyExceptionContext
{
// ExceptionHandler注解用来处理异常信息
@ExceptionHandler(value = {Exception.class})
// 方法参数Exception会自动名将错误信息封装到Exception中
public ModelAndView exception1(Exception ex){
// 创建ModelAndView并设置跳转页面
ModelAndView modelAndView= new ModelAndView("myexcepton");
// 将错误信息存放在request域当中
modelAndView.addObject("ex",ex);
System.out.println("Exception错误类运行.....");
return modelAndView;
}
// 具体的错误信息
@ExceptionHandler(value = {ArithmeticException.class})
public ModelAndView exception2(Exception ex){
ModelAndView modelAndView= new ModelAndView("myexcepton");
modelAndView.addObject("ex",ex);
System.out.println("ArithmeticException错误类运行.....");
return modelAndView;
}
}
index.jsp 首页
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="exhandle1" method="post">
<input type="text" name="i"/>
<input type="submit" value="异常演示"/>
</form>
</body>
</html>
myexcepton.jsp (错误跳转页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>错误页面</h2>
<h4>具体的错误信息为:</h4> ${requestScope.ex}
</body>
</html>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<welcome-file-list>
<welcome-file>index1.jsp</welcome-file>
</welcome-file-list>
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<!-- 指定springmvc配置文件的位置 -->
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-springmvc.xml</param-value>
</init-param>
<!-- load-on-startup服务器启动的时候创建对象,值越小 优先级越高-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!-- / 拦截所有请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 设置编码格式 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<!-- 设置请求乱码-->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!-- 设置响应乱码 -->
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
applicationContext-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:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<context:component-scan base-package="com.stone.mvc"/>
<!-- 配置视图解析器 InternalResourceViewResolver-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- prefix配置前置 -->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!-- suffix配置后置 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- springmvc 高级注解驱动 -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
</beans>
在MyController类中计算10/i 等于多少 ,当i传过来为0时就会引发ArithmeticException异常中的 by zero错误信息,而此时的信息,会被标有@ExceptionHandler方法中指定异常类型的信息捕获并进行处理
@ExceptionHandler细节点 :
- 给方法上添加Exception他会自动捕获异常并存储
- 如果有多个@ExceptionHandler注解的方法,精确优先
- 处理异常类(全部写处理异常的方法的类)的注解必须是@ControllerAdvice
- 异常处理类和异常发生的本类都写了异常处理方法,本类优先异常处理类
@ResponseStatus 案例
用户进行登录校验,查看是否有admin用户的用户名,如果没有查找就报自定义异常的错误信息。
自定义异常
用户登录业务
用户登录页面
@ResponseStatus细节点
- 在使用@ResponseStatus注解时,如果有@ExceptionHandler注解的使用,并且能捕捉对应的异常信息,那么会跳过@ResponseStatus自定义异常。
SimpleMappingExceptionResolver 配置方式
配置方法
Controller类
测试页面
测试结果
SimpleMappingExceptionResolver配置细节点
- 他的优先级是记录的三者中优先级最低得