日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring MVC错误处理流程

發布時間:2023/12/3 javascript 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring MVC错误处理流程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用Spring MVC可以通過三種方式處理異常流,其目的是攔截任何應用程序異常,并向用戶提供友好而信息豐富的視圖。

1.在web.xml文件中使用error-page標記:

這是servlet規范驅動的方法,其中基于HTTP響應代碼或異常類型來攔截從應用程序冒出的異常,并使用location子標記通過以下方式指定該異常的處理程序:

<error-page><error-code>500</error-code><location>/500</location> </error-page> <error-page><exception-type>java.lang.Exception</exception-type><location>/uncaughtException</location> </error-page>

如果它是基于Spring MVC的應用程序,并且意圖是通過Spring MVC視圖呈現消息,那么理想情況下,一個位置應該是可以顯示內容的Spring控制器,并且可以使用上面的2個位置來完成此操作Spring MVC配置:

<mvc:view-controller path="/500" view-name="500view"/> <mvc:view-controller path="/uncaughtException" view-name="uncaughtexception"/>

2.注冊一個

HandlerExceptionResolver負責將異常映射到視圖,最簡單的方法是注冊一個SimpleMappingExceptionResolver,它可以將異常類型映射到視圖名稱。 以下是使用Spring xml bean定義(基于Roo示例)注冊SimpleMappingExceptionResolver的方法:

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" p:defaultErrorView="uncaughtException"><property name="exceptionMappings"><props><prop key=".DataAccessException">dataAccessFailure</prop><prop key=".NoSuchRequestHandlingMethodException">resourceNotFound</prop><prop key=".TypeMismatchException">resourceNotFound</prop><prop key=".MissingServletRequestParameterException">resourceNotFound</prop></props></property> </bean>

或使用基于Java配置的bean定義:

@Bean public HandlerExceptionResolver handlerExceptionResolver() {SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver();exceptionResolver.setDefaultErrorView("uncaughtException");Properties mappings = new Properties();mappings.put(".DataAccessException", "dataAccessFailure");mappings.put(".NoSuchRequestHandlingMethodException", "resourceNotFound");mappings.put(".TypeMismatchException", "resourceNotFound");mappings.put(".MissingServletRequestParameterException", "resourceNotFound");exceptionResolver.setExceptionMappings(mappings );return exceptionResolver; }

3.使用@ExceptionHandler

這是我的首選方法,使用@ExceptionHandler批注有兩種變體。

在第一個變體中,可以將@ExceptionHandler應用于控制器類的級別,在這種情況下,同一控制器@RequestMapped方法引發的異常由@ExceptionHandler注釋方法處理。

@Controller public class AController {@ExceptionHandler(IOException.class)public String handleIOException(IOException ex) {return "errorView";} }

在@ExceptionHandler的第二個變體中,可以通過對@ControllerAdvice帶注釋的類的方法進行注釋,將注釋應用于所有控制器類:

@ControllerAdvice public class AControllerAdvice {@ExceptionHandler(IOException.class)public String handleIOException(IOException ex) {return "errorView";} }

這些本質上是在Spring MVC應用程序中處理應用程序異常的方法。

參考: Spring MVC來自all和各式博客的JCG合作伙伴 Biju Kunjummen的錯誤處理流程 。

翻譯自: https://www.javacodegeeks.com/2013/06/spring-mvc-error-handling-flow.html

總結

以上是生活随笔為你收集整理的Spring MVC错误处理流程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。