1. 程式人生 > >spring mvc 異常狀態

spring mvc 異常狀態

spring mvc 異常狀態

spring mvc中的404:

1.mappedHandler找不到

response.sendError(HttpServletResponse.SC_NOT_FOUND);


spring mvc異常機制

1.mappedHandler找不到 並且throwExceptionIfNoHandlerFound設定為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:

web.xml的配置Dispatcher的此參數為true

					<init-param>
						<param-name>throwExceptionIfNoHandlerFound</param-name>
						<param-value>true</param-value>
					</init-param>

2.判斷返回視圖層之前是否發生了異常,主要有兩種HandlerExceptionResolver、ModelAndViewDefiningException

3.如沒有異常,則進行視圖渲染

通過viewResolver找出相應的view,進行具體的模板渲染

通過不同的View實現AbstractView的renderMergedOutputModel方法

Exception if there's a problem rendering the view

如果ViewResolver匹配不到對應的View,則拋出ServletException

ServletException:if view is missing or cannot be resolved


spring mvc 異常處理

1.HandlerExceptionResolver

只能處理請求過程中拋出的異常

不處理異常處理本身所拋出的異常和視圖解析過程中拋出的異常

2.@ControllerAdvice的@ExceptionHandler註解


spring mvc 異常狀態