Spring Global exception capture implements the Handlerexceptionresolver interface, which captures exceptions in the Error-page configuration __javaweb

Source: Internet
Author: User
Tags object object
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.












Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.