In the project, how do I handle exceptions and write code to catch exceptions wherever exceptions may occur? This is obviously unreasonable. When the project grows, it cannot be maintained. So how can we ensure that the Code for exception handling is streamlined and easy to maintain? This is what we will talk about in this article-> exception handling.
In Spring MVC, we can handle exceptions in the following two ways:
1. inherit the HandlerExceptionResolver interface to implement your own processing methods, such:
Public class MyHandlerExceptionResolver implements HandlerExceptionResolver {@ Override public ModelAndView resolveException (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {// Add your own Exception handling logic, such as logging // TODO Auto-generated method stub return new ModelAndView ("exception ");}}
Add the following in the project configuration file:
<Bean id = "exceptionResolver" class = "package name. MyHandlerExceptionResolver"/>
This completes exception capturing and handling.
II. we introduced the first way to catch and handle exceptions, but the first method needs to be configured in the configuration file. Sometimes we may feel that the content of the configuration file is too messy, then, we can use the @ ExceptionHandler annotation to capture and handle zero-configuration exceptions.
First, create a parent class BaseController for the controller in the com. demo. web. controllers package of our project. The content is as follows:
Package com. demo. web. controllers; import java. SQL. SQLException; import javax. servlet. http. httpServletRequest; import org. springframework. web. bind. annotation. exceptionHandler; public abstract class BaseController {@ ExceptionHandler public String exception (HttpServletRequest request, Exception e) {// Add your own exception handling logic, such as logging request. setAttribute ("exceptionMessage", e. getMessage (); // handle different if (e instanceof SQLException) return "testerror"; else return "error" ;}} based on different exception types ";}}
Second, modify HelloWorldController in the project to inherit from BaseController for testing:
Public class HelloWorldController extends BaseController {//... content omitted}
Then, modify the index method in HelloWorldController to throw an exception and check whether the exception can be captured properly:
// @ AuthPassport @ RequestMapping (value = {"/index", "/hello"}) public ModelAndView index () throws SQLException {throw new SQLException ("database exception. ");/* ModelAndView modelAndView = new ModelAndView (); modelAndView. addObject (" message "," Hello World! "); ModelAndView. setViewName (" index "); return modelAndView ;*/}
Finally, add the testerror. jsp View to the views folder to display the error message:
<%@ 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">
Run the project:
We can see that the exception has been captured and displayed. In this way, as long as all our other controllers are inherited from BaseController, the exception can be captured and handled in a centralized manner.
Code download: http://pan.baidu.com/s/1c0eTseG