Way One
1.1 Creating a global Exception class
This class implements the Handlerexceptionresolver interface in detail as follows:
Package com.rbc.hbms.util;
Import Java.util.HashMap;
Import Java.util.Map;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.springframework.web.servlet.HandlerExceptionResolver;
Import Org.springframework.web.servlet.ModelAndView;
/** * Captures global exceptions and outputs * @author Moxingjian * @email 939697374@qq.com * @date August 16, 2017 Morning 10:01:40 * @version 1.0 * * public class Gobalexceptionresolver implements handlerexceptionresolver{@Override public Modelandview resolveexcep tion (httpservletrequest request, HttpServletResponse response, Object object, Exception ex) {String message = Ex.get
Message ();
Throwable cause = Ex.getcause ();
SYSTEM.OUT.PRINTLN ("Into global anomaly");
The following information is generally stored in the log, here as a simple test System.out.println ("Exception type =" + Ex.getclass (). Getsimplename ());
SYSTEM.OUT.PRINTLN ("Exception description =" + message);
System.out.println ("Abnormal reason =" + cause);
/** * Test When you can output the error message to the page, real to the customer when used, * do not do so, to impress others, others think your system is always a problem. * Can be properly prompted "system busy" or "youAccess to the page does not exist "or something.
* * map<string, object> model = new hashmap<string, object> ();
Model.put ("Ex", Ex);
/** * can jump to different pages depending on your needs and different exceptions.
* For example, DAO layer exceptions, you can skip to the database Exceptions page.
* Service layer exception, you can jump to the "Business Exception page." * Unable to find the page exception, you can jump to 404 pages.
And so on/return new Modelandview ("error", model);
}
}
1.2 Configuration
Configure this class in the spring configuration file
<!--Global exception configuration-->
<bean id= "Gobalexceptionresolver" class= "Com.rbc.hbms.util.GobalExceptionResolver"/ >
1.3 Test
Catch an unhandled exception at the service layer
int i = 10/0;
The effect is as follows:
Mode two
Optimize the 2.1 configuration access Path on the basis of mode one
First, the access path is written in the class name and method name respectively
Class name
@Controller
@RequestMapping ("/sysmgr/gobalexceptionresolver.action") Public
class Gobalexceptionresolver implements Handlerexceptionresolver {
Method
@Override
@RequestMapping (params = "method=resolveexception") public
Modelandview Resolveexception ( HttpServletRequest request, HttpServletResponse response, Object object,
Exception ex) {
2.2 Exception Capture
In the Web.xml configuration, the following
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/ Sysmgr/gobalexceptionresolver.action?method=resolveexception</location>
</error-page>
As long as it is under that exception, it will be found through the path.