52SpringMVC - exception handler

The exception information in the processing of spring MVC requests is handled by the exception handler. The custom exception handler can implement the exception handling logic of a system.

Abnormal handling ideas

There are two types of exceptions in the system: expected exception and runtime exception. The former obtains exception information by catching exceptions, while the latter reduces the occurrence of runtime exceptions by standardizing code development and testing.

The dao, service and controller of the system are all thrown upward through throws Exception. Finally, the spring MVC front-end controller hands over to the exception processor for exception handling, as shown in the following figure:

Custom exception class

In order to distinguish different exceptions, usually according to the exception type, here we create a custom system exception.
If the controller, service and dao throw such an exception, it means that the system expects to handle the exception information.

public class MyException extends Exception {
	// Abnormal information
	private String message;

	public MyException() {
		super();
	}

	public MyException(String message) {
		super();
		this.message = message;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}

Custom exception handler

public class CustomHandleException implements HandlerExceptionResolver {

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception exception) {
		// Define exception information
		String msg;

		// Judge the type of abnormality
		if (exception instanceof MyException) {
			// If it is a custom exception, read the exception information
			msg = exception.getMessage();
		} else {
			// If it is a runtime exception, take the error stack and get the exception information from the stack
			Writer out = new StringWriter();
			PrintWriter s = new PrintWriter(out);
			exception.printStackTrace(s);
			msg = out.toString();

		}

		// Send error information to relevant personnel, e-mail, SMS and other ways
		// TODO

		// Return to error page and display error information to user-friendly page
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("msg", msg);
		modelAndView.setViewName("error");

		return modelAndView;
	}
}

Exception handler configuration

Add in springmvc.xml:

<! -- configure global exception handler -- >
<bean  id="customHandleException" 	class="cn.itcast.ssm.exception.CustomHandleException"/>

Error page

<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<h1>The system is abnormal!</h1>
	<br />
	<h1>Abnormal information</h1>
	<br />
	<h2>${msg }</h2>

</body>
</html>

Abnormality test

Modify the ItemController method "queryItemList" and throw an exception:

/**
 * Query product list
 * 
 * @return
 * @throws Exception
 */
@RequestMapping(value = { "itemList", "itemListAll" })
public ModelAndView queryItemList() throws Exception {
	// Custom exception
	if (true) {
		throw new MyException("Custom exception occurred~");
	}

	// Runtime exception
	int a = 1 / 0;

	// Query product data
	List<Item> list = this.itemService.queryItemList();
	// Create ModelAndView and set logical view name
	ModelAndView mv = new ModelAndView("itemList");
	// Put the product data into the model
	mv.addObject("itemList", list);

	return mv;
}

Tags: Spring xml Java

Posted on Fri, 29 Nov 2019 21:19:10 -0800 by khaitan_anuj