Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use SpringMVC exception to handle SimpleMappingExceptionResolver class in Java

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly describes how to use SpringMVC exception handling SimpleMappingExceptionResolver class in Java, the article is very detailed, has a certain reference value, interested friends must read!

Spring 3.0 handles exceptions in two ways:

One is to use the HandlerExceptionResolver interface, and Spring already provides the default implementation class SimpleMappingExceptionResolver.

The second method is implemented internally in Controller, which is more flexible.

According to the current survey results, these two methods cannot coexist. We generally use the first method in our projects.

The following describes the two ways of using:

I. Methods based on HandlerExceptionResolver interface

To use this method, you only need to implement the resolveException method, which returns a ModelAndView object, determines the type of exception inside the method, and then returns the appropriate ModelAndView object. If the method returns null, Spring will continue to look for other beans that implement the HandlerExceptionResolver interface. In other words, Spring searches for all beans registered in its environment that implement the HandlerExceptionResolver interface, executing one by one until it returns a ModelAndView object.

public class CustomExceptionHandler implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception) { if(exception instanceof IOException){ return new ModelAndView("ioexp"); }else if(exception instanceof SQLException){ return new ModelAndView("sqlexp"); } return null; } }

This class must be declared in the Spring configuration file or let Spring manage it using the @Component tag. Spring also provides the default implementation class SimpleMappingExceptionResolver, which can be declared by injecting into Spring configuration files when needed. Custom implementation classes and default implementation classes can be used together.

Examples are as follows:

error/ioexp error/sqlexp

A typical exception display is as follows:

Exception! Exception:

Exception is stored in the request in SimpleMappingExceptionResolver, you can check the source code for details.

In addition, the exception display interface configured here only includes the main file name, and the file path and suffix have been specified in viewResolver. If the page cannot be found, the page path will be adjusted according to the error prompt.

Second, the Controller is implemented separately

This method needs to be defined inside Controller, and then create a method decorated with @ExceptionHandler annotation to handle exceptions. This method is basically similar to the method decorated with @RequestMapping, except that one more parameter of type Exception can be added.@ExceptionHandler can add one or more exception types. If it is empty, it is considered that all exception type errors can be triggered.

@Controller public class ExceptionHandlerController { @ExceptionHandler(value={IOException.class,SQLException.class}) public String exp(Exception ex,HttpServletRequest request) { request.setAttribute("ex", ex); return "/error.jsp"; } } III. Related issues

Is there a conflict between HandlerExceptionResolver and error-page configured in web. xml?

The error-page configured in web.xml is also the page displayed when an error occurs in the configuration:

500 /500.jsp

If resolveException returns ModelAndView, it will be displayed preferentially based on the page in the returned value. However, resolveException can return null, in which case the error-page 500 status code configuration page in web.xml is displayed.

Explanation of return values in API documentation:

return a corresponding ModelAndView to forward to, or null for default processing. Above is "How to use SpringMVC exception handling SimpleMappingExceptionResolver class in Java" All the contents of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report