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 handle exceptions in spring mvc

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "how to handle exceptions in spring mvc". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to deal with exceptions in spring mvc".

404 in spring mvc:

1.mappedHandler can't find it.

Response.sendError (HttpServletResponse.SC_NOT_FOUND)

Spring mvc exception mechanism

1.mappedHandler cannot be found and throwExceptionIfNoHandlerFound is set to true

ThrowExceptionIfNoHandlerFound

Source:

If (this.throwExceptionIfNoHandlerFound) {throw new NoHandlerFoundException (request.getMethod (), getRequestUri (request), new ServletServerHttpRequest (request). GetHeaders ()) } else {response.sendError (HttpServletResponse.SC_NOT_FOUND);}

Config:

This parameter of the configuration Dispatcher of web.xml is true

ThrowExceptionIfNoHandlerFound true

two。 There are two main types of HandlerExceptionResolver and ModelAndViewDefiningException to determine whether an exception has occurred before returning to the view layer.

3. If there is no exception, the view is rendered.

Find out the corresponding view through viewResolver and render the specific template.

The renderMergedOutputModel method of realizing AbstractView through different View

Exception if there's a problem rendering the view

If the ViewResolver does not match the corresponding View, the ServletException is thrown

ServletException:if view is missing or cannot be resolved

Spring mvc exception handling

1.HandlerExceptionResolver

Only exceptions thrown during the request can be handled

Do not handle the exception thrown by the exception handler itself and the exception thrown during view parsing

@ ExceptionHandler comment on 2.@ControllerAdvice

3.1and 2 can only handle exceptions before view. If it is a view layer rendering exception, it needs to be handled separately.

one。 Using filter to catch exceptions other than 1 and 2

Public class ErrorHandlerFilter implements Filter {ErrorHandler errorHandler; @ Override public void doFilter (ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {try {filterChain.doFilter (request, response) } catch (Exception ex) {/ / call ErrorHandler and dispatch to error jsp String errorMessage = errorHandler.handle (request, response, ex); request.setAttribute ("errorMessage", errorMessage) Request.getRequestDispatcher ("/ WEB-INF/jsp/error/dispatch-error.jsp") .forward (request, response) } @ Override public void init (FilterConfig filterConfig) throws ServletException {errorHandler = (ErrorHandler) WebApplicationContextUtils .getRequiredWebApplicationContext (filterConfig.getServletContext ()) .getBean ("defaultErrorHandler") } / /...}

two。 Catch an exception from writing dispatchServlet to doservice

Public class DispatcherServletHandler extends DispatcherServlet {private static final String ERROR = "error"; private static final String VIEW_ERROR_PAGE = "/ WEB-INF/views/error/view-error.jsp" @ Override protected void doService (HttpServletRequest request, HttpServletResponse response) throws Exception {try {super.doService (request, response) } catch (Exception ex) {request.setAttribute (ERROR, ex); request.getRequestDispatcher (VIEW_ERROR_PAGE) .forward (request, response) } above is all the content of the article "how to handle exceptions in spring mvc". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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