Springmvc learning notes (16)-exception Processor

Source: Internet
Author: User

Springmvc learning notes (16)-exception Processor
Springmvc learning notes (16)-exception Processor

This article describes how to handle exceptions in springmvc and how to customize exception handling classes and configure a global exception processor.

Exception Handling ideas

There are two types of exceptions in the system:

Expected RuntimeException

The former acquires exception information by capturing exceptions, and the latter mainly reduces the occurrence of runtime exceptions by standardizing code development and testing.

The dao, service, and controller of the system are thrown up through throws Exception. The springmvc front-end controller is used by the Exception processor for Exception handling, for example:

Springmvc provides a global exception processor (one system has only one exception processor) for unified exception handling.

Custom exception class

Defines Exception classes for different Exception types and inherits Exception.

Package com. iot. learnssm. firstssm. exception;/*** Created by brian on 2016/3/7. ** system-defined Exception class. For the expected Exception, you must throw this type of Exception in the Program */public class CustomException extends Exception {// The Exception message is public String message; public CustomException (String message) {super (message); this. message = message;} public String getMessage () {return message;} public void setMessage (String message) {this. message = message ;}}
Global exception Processor

Ideas:

If an exception occurs in the system, it is thrown manually in the program. dao is thrown to the service, service, controller, and controller to the front-end controller. The front-end controller calls the global exception processor.

Troubleshooting of global exception processors:

Parse the exception type

If the exception type is a system-defined exception, retrieve the exception information directly. The error page shows that if the exception type is not a system-defined exception, construct a custom exception type (the information is "Unknown error ")

Springmvc providesHandlerExceptionResolverInterface

Public ModelAndView resolveException (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {// handler is the Handler Object to be executed by the processor adapter (only method) // parse the exception type // If the exception type is a system-defined exception, retrieve the exception information directly. The error page displays // String message = null; // if (ex instanceof CustomException) {// message = (CustomException) ex ). getMessage (); //} else {// If the exception type is not a custom exception, construct a custom exception type (the message is "Unknown error ") // message = "Unknown error"; // The code above changes to 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 (); // upload the error message to modelAndView on the page. addObject ("message", message); // points to the error page modelAndView. setViewName ("error"); return modelAndView ;}}
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" %>
${message} Configure the global exception processor in springmvc. xml

  
  

There is only one global exception processor, and it is useless to configure multiple.

Exception Test

You must manually throw an exception at any location in controller, service, and dao. If the exception is thrown manually in the program, the custom exception information is displayed on the error page. If the exception is not thrown manually, it indicates a runtime exception, only "Unknown error" is displayed on the error page ".

An exception is thrown in the controller Method of item modification.
Public String editItems (Model model, @ RequestParam (value = "id", required = true) Integer items_id) throws Exception {// call service to query the item information ItemsCustom itemsCustom = itemsService Based on the item id. findItemsById (items_id); // determines whether the item is empty. if no item is found based on the id, an exception is thrown, prompting that the user's item information does not exist if (itemsCustom = null) {throw new CustomException ("the modified item information does not exist! ");} // Upload the model data to the page through the model in the form parameter // equivalent to modelAndView. addObject method model. addAttribute ("items", itemsCustom); return "items/editItems ";}
An exception is thrown in the service interface:
Public ItemsCustom findItemsById (Integer id) throws Exception {Items items = itemsMapper. selectByPrimaryKey (id); if (items = null) {throw new CustomException ("the modified item information does not exist! ");} // Process the product information in the middle //.... // return ItemsCustom itemsCustom = null; // copy the value of items to itemsCustom if (items! = Null) {itemsCustom = new ItemsCustom (); BeanUtils. copyProperties (items, itemsCustom);} return itemsCustom ;}
If the exception is related to business functions, it is recommended to throw an exception in the service. An exception that has nothing to do with business functions. It is recommended to throw it in the controller.

We recommend that you throw an exception in the service.

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.