SpringMVC Exception Handling
To handle exceptions during code running in a unified manner and provide a user-friendly exception interface, springMVC's exception handling function needs to be introduced. To demonstrate this function, this article provides a common requirement.
All exceptions are classified into two categories: one is the exception class created by the programmer and the other is the exception class defined by the system or framework. The exception class defined by the programmer outputs the exception information on the interface, and all the exceptions defined by the system output "unknown errors" in a unified manner ".
When an exception is thrown, the page jumps to the exception page and reads the page in seconds. After three seconds, the page automatically jumps to the page where the request occurs, or the page defined by the programmer.
To implement this function, first write your own exception class. Here I name it MyException and add two member variables: message and destination, indicates the exception information and the target page that automatically jumps to the exception page.
Add getter, setter, and constructor. The program list is as follows:
Package com. elin4it. ssm. controller. exception;/*** Description: MyException * Author: Elin Zhou * Create: */public class MyException extends Exception {private String message; // The position to jump after an exception occurs, by default, the private String destination; public MyException () {} public MyException (String message) {super (message); this. message = message;} public MyException (String message, String destination) {super (message); this. destination = destination; this. message = message;} public String getDestination () {return destination;} public void setDestination (String destination) {this. destination = destination;} @ Override public String getMessage () {return message;} public void setMessage (String message) {this. message = message ;}}
SpringMVC provides the HandlerExceptionResolver interface for programmers to handle exceptions. As long as the resolveException method is implemented, this method can be called to handle exceptions when exceptions occur.
Create a class to implement the resolveException interface and add
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e)
Method.
We will not repeat the handling of specific exceptions here. This article mainly aims to introduce the exception handling process. The specific business logic depends on the actual situation. Here we only paste the Code implemented to fulfill the above requirements.
Package com. elin4it. ssm. controller. exception; import org. springframework. web. servlet. handlerExceptionResolver; import org. springframework. web. servlet. modelAndView; import javax. servlet. http. httpServletRequest; import javax. servlet. http. break;/*** Description: MyExceptionResolver * Author: Elin Zhou * Create: */public class implements failed {@ Override public ModelAndView resolveException (HttpServletRequest httpServletRequest, too many, object o, Exception e) {MyException myException = null; if (e instanceof MyException) {// if the Exception received is a user-defined Exception, myException = (MyException) e ;} else {// if the exception received is another system-defined exception, myException = new MyException (unknown error);} if (myException. getDestination () = null) {// if no target page is specified after an exception occurs, the page that initiates the request is automatically redirected. // obtain the complete path of the request, such as http: // localhost: 8080/ssm/book/bookIndex String requestURL = httpServletRequest. getHeader (Referer); // splits the path by '/'. String [] splitFromURL = requestURL. split (/); // obtain the number of split substrings int count = splitFromURL. length; // obtain the last two terminal strings and splice them into the logical address String destination = splitFromURL [count-2] +/+ splitFromURL [count-1]; myException. setDestination (destination);} ModelAndView modelAndView = new ModelAndView (); // you can send an exception to the request to jump to modelAndView. addObject (exception, myException); modelAndView. setViewName (error); return modelAndView ;}}
The code above determines the exception type based on the parameter. If it is defined by the system, a MyException is created and the message is assigned an "Unknown error ". Then, judge whether the destination is null. If so, assign the value to the page on which the user initiates the request. Assign the value to ModelAndView and return the value.
Finally, you need to configure the exception processor to springmvc and add the injection of the processor class to the springmvc configuration file.
Do not write the id or configure other information. When Spring MVC loads beans to the IOC container, If It scans the class that implements the HandlerExceptionResolver interface, it serves as the exception processor.
Finally, paste the controller method for testing and the jsp page for exception handling.
@ RequestMapping (/addBook) public String addBook () throws Exception {// test custom Exception if (1 = 1) throw new MyException (test Exception ); // test the system-defined exception int a = 10/0; return book/addBook ;}
<% -- Created by IntelliJ IDEA. user: elin Date: 15-7-4 Time: To change this template use File | Settings | File Templates. -- %> <% @ page contentType = text/html; charset = UTF-8 language = java %>
<Script type = text/javascript> function countDown (secs, surl) {var jumpTo = document. getElementById ('jumpto'); jumpTo. innerHTML = secs; if (-- secs> 0) {setTimeout (countDown (+ secs +, '+ surl +'), 1000);} else {location. href = surl;-ma }}</script>
${exception.message}