In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to implement the SpringMvc exception handler". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to implement the SpringMvc exception handler.
Brief introduction of SpringMvc exception Handler
The exception information in the process of handling the request in SpringMvc is handled by the exception handler, and the custom exception handler can realize the exception handling logic of a system.
Abnormal understanding
Exceptions include compile-time exceptions and run-time exceptions, where compile-time exceptions are also called expected exceptions. Runtime exceptions are found only when the project is running, and you don't need to worry about it at compile time.
Run-time exceptions, such as null pointer exceptions and array out-of-bounds exceptions, can only be solved by programmers' rich experience and testers' continuous rigorous testing.
Compile-time exceptions, such as database exceptions, file read exceptions, custom exceptions, etc. For such an exception, you must use try catch code blocks or the throws keyword to handle the exception.
Exception handling thought
There are two types of exceptions in the system: expected exception (compile-time exception) and run-time exception RuntimeException. The former obtains exception information by capturing exceptions, while the latter mainly reduces the occurrence of run-time exceptions by means of standard code development and testing.
The dao, service and controller of the system are all thrown up through throws Exception, and finally the SpringMvc front-end controller hands it to the exception handler for exception handling, as shown below:
The global scope has only one exception handler.
First step of customizing exception classes: CustomException.javapackage com.cyb.ssm.exception;/** * customize compile-time exceptions * * @ author apple * * / public class CustomException extends Exception {private String msg; public String getMsg () {return msg;} public void setMsg (String msg) {this.msg = msg;} public CustomException (String msg) {super (); this.msg = msg }} step 2: CustomExceptionResolver.java (focus) package com.cyb.ssm.resolver;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerExceptionResolver;import org.springframework.web.servlet.ModelAndView;import com.cyb.ssm.exception.CustomException;public class CustomExceptionResolver implements HandlerExceptionResolver {@ Override public ModelAndView resolveException (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {String message= "" / / exception handling logic if (ex instanceof CustomException) {message= ((CustomException) ex). GetMsg ();} else {message= "unknown error";} ModelAndView mv=new ModelAndView (); mv.setViewName ("error"); mv.addObject ("message", message); return mv Step 3: add bean to springmvc.xml step 4: jsp error page error page ${message} step 5: test class @ RequestMapping ("queryItem") public ModelAndView queryItem () throws CustomException {/ / query the database Simulate List itemList = Service.queryItemList () with static data ModelAndView mvAndView = new ModelAndView (); mvAndView.addObject ("itemList", itemList); / / set view (logical path) mvAndView.setViewName ("item/item-list"); if (true) {throw new CustomException ("I am a custom exception class");} return mvAndView;}
Thank you for reading, the above is the content of "how to implement the SpringMvc exception handler". After the study of this article, I believe you have a deeper understanding of how to implement the SpringMvc exception handler, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.