Home >Java >javaTutorial >springMVC support for exception handling
No matter what project you are doing, exception handling is very necessary, and you cannot throw some error codes that only programmers can understand to users, so at this time, perform unified exception handling to show a comparison Friendly error pages are necessary. Like other MVC frameworks, springMVC also has its own exception handling mechanism.
There are two main ways to handle exceptions provided by springMVC. One is to directly implement your own HandlerExceptionResolver. Of course, this also includes using the SimpleMappingExceptionResolver and DefaultHandlerExceptionResolver that Spring has provided us. The other is to use annotations to implement a specialized Controller for handling exceptions - ExceptionHandler.
1. Implement your own HandlerExceptionResolver. HandlerExceptionResolver is an interface. springMVC itself already has its own implementation-DefaultHandlerExceptionResolver. This resolver is just for Some of the more typical exceptions are intercepted and then the corresponding error codes are returned. Of course, you can also inherit the DefaultHandlerExceptionResolver class and then rewrite some of the exception handling methods to implement your own exception handling.
The fourth of the above resolveException The parameter indicates which type of exception is handled. Because the Exception class is the base class of all exception classes, if you want to perform different processing according to different exception types, you can perform different processing according to different exception types in the resolveException method and return different exception views. For example:
public class ExceptionHandler implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { // TODO Auto-generated method stub if (ex instanceof NumberFormatException) { //doSomething... return new ModelAndView("number"); } else if (ex instanceof NullPointerException) { //doSomething... return new ModelAndView("null"); } return new ModelAndView("exception"); } }
After defining such an exception handler, you must define such a bean object in the applicationContext, such as:
nbsp;HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<base>">
<title>My JSP 'number.jsp' starting page</title>
<meta>
<meta>
<meta>
<meta>
<meta>
<!--
<link rel="stylesheet" type="text/css" href="styles.css?1.1.11">
-->
NumberFormatException. <br>
<br>
<br><span><!-- 这是JSP中的内置对象exception --></span>
<br><span><!-- 这是SpringMVC放在返回的Model中的异常对象 --></span>
<span><!-- HttpServletResponse返回的错误码信息,因为前面已经配置了NumberFormatException的错误码返回值为888,所以这里应该显示888 --></span>
(4)当请求/test/number.do的时候会返回定义好的number视图,返回结果如下:
2、使用@ExceptionHandler进行处理
使用@ExceptionHandler进行处理有一个不好的地方是进行异常处理的方法必须与出错的方法在同一个Controller里面
如:
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import com.tiantian.blog.web.servlet.MyException; @Controller public class GlobalController { /** * 用于处理异常的 * @return */ @ExceptionHandler({MyException.class}) public String exception(MyException e) { System.out.println(e.getMessage()); e.printStackTrace(); return "exception"; } @RequestMapping("test") public void test() { throw new MyException("出错了!"); } }
这里在页面上访问test方法的时候就会报错,而拥有该test方法的Controller又拥有一个处理该异常的方法,这个时候处理异常的方法就会被调用
优先级
既然在SpringMVC中有两种处理异常的方式,那么就存在一个优先级的问题:
当发生异常的时候,SpringMVC会如下处理:
(1)SpringMVC会先从配置文件找异常解析器HandlerExceptionResolver
(2)如果找到了异常异常解析器,那么接下来就会判断该异常解析器能否处理当前发生的异常
(3)如果可以处理的话,那么就进行处理,然后给前台返回对应的异常视图
(4)如果没有找到对应的异常解析器或者是找到的异常解析器不能处理当前的异常的时候,就看当前的Controller中有没有提供对应的异常处理器,如果提供了就由Controller自己进行处理并返回对应的视图
(5)如果配置文件里面没有定义对应的异常解析器,而当前Controller中也没有定义的话,那么该异常就会被抛出来。
The above is the detailed content of springMVC support for exception handling. For more information, please follow other related articles on the PHP Chinese website!