The dao, service and controller of the system are all thrown upward through throws Exception, and finally the spring MVC front-end controller is handed over to the exception processor for processing. Spring MVC provides a global exception handler (only one exception handler in a system) for unified exception handling.
Custom exception class
Define Exception classes for different Exception types, and inherit Exception.
package com.iot.learnssm.firstssm.exception; /** * Custom exception class for expected exception */ public class CustomException extends Exception{ //Abnormal information public String message; public CustomException(String message){ this.message = message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
Global exception handler
Spring MVC provides a HandlerExceptionResolver interface
public class CustomExceptionResolver implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { CustomException customException; if (ex instanceof CustomException) { customException = (CustomException) ex; } else { customException = new CustomException("unknown error"); } //error message String message = customException.getMessage(); ModelAndView modelAndView = new ModelAndView(); //Send error message to page modelAndView.addObject("message", message); //Point to error page modelAndView.setViewName("error"); return modelAndView; } }
- Handler is the handler object to be executed by the processor adapter (only method)
Error page
<%-- Created by IntelliJ IDEA. User: Brian Date: 2016/3/4 Time: 10:51 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Error prompt</title> </head> <body> ${message} </body> </html>
Configuring the global exception handler in springmvc.xml
<! -- the global exception handler is the global exception handler as long as it implements the HandlerExceptionResolver interface -- > <bean class="com.iot.learnssm.firstssm.exception.CustomExceptionResolver"></bean>
There is only one global exception handler, and it is useless to configure multiple.
Abnormality test
Exceptions need to be thrown manually at any place in the controller, service and dao. If the exception is manually thrown in the program, the user-defined exception information will be displayed in the error page. If the exception is not manually thrown, it means that it is a runtime exception, and only "unknown error" will be displayed in the error page.
- An exception is thrown in the controller method of the commodity modification
public String editItems(Model model,@RequestParam(value="id",required=true) Integer items_id)throws Exception { //Call service to query commodity information according to commodity id ItemsCustom itemsCustom = itemsService.findItemsById(items_id); //Judge whether the product is empty. If no product is found according to the id, throw an exception and prompt the user that the product information does not exist if(itemsCustom == null){ throw new CustomException("The modified product information does not exist!"); } //Transfer the model data to the page through the model in the parameter model.addAttribute("items", itemsCustom); return "items/editItems"; }
- Throw an exception in the service interface:
public ItemsCustom findItemsById(Integer id) throws Exception { Items items = itemsMapper.selectByPrimaryKey(id); if(items==null){ throw new CustomException("The modified product information does not exist!"); } //Return to ItemsCustom ItemsCustom itemsCustom = null; //Copy the property value of items to itemsCustom if(items!=null){ itemsCustom = new ItemsCustom(); BeanUtils.copyProperties(items, itemsCustom); } return itemsCustom; }
- If there is an exception related to the business function, it is recommended to throw an exception in the service.
- Exceptions not related to business functions are recommended to be thrown in the controller.
It is recommended to throw an exception in the service.