溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringMVC基于配置的異常處理器怎么用

發(fā)布時間:2022-05-30 10:03:35 來源:億速云 閱讀:156 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了SpringMVC基于配置的異常處理器怎么用的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇SpringMVC基于配置的異常處理器怎么用文章都會有所收獲,下面我們一起來看看吧。

    一、基于配置的異常處理

    SpringMVC 提供了一個處理控制器方法執(zhí)行過程中所出現(xiàn)的異常的接口:HandlerExceptionResolver。

    HandlerExceptionResolver接口的實(shí)現(xiàn)類有:

    DefaultHandlerExceptionResolver,這個是默認(rèn)使用的處理器,之前遇到的一些異常,其實(shí)springMVC 都已經(jīng)給我們處理過了。

    SimpleMappingExceptionResolver,這個可以讓我們自定義異常處理。當(dāng)出現(xiàn)指定的異常,可以設(shè)置返回新的視圖。

    使用SimpleMappingExceptionResolver,在springMVC的配置文件中:

    <!--配置異常處理-->
      <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
          <property name="exceptionMappings">
              <props>
                  <prop key="java.lang.ArithmeticException">error</prop>
              </props>
          </property>
      </bean>

    示例里使用的一個處理運(yùn)算異常的類ArithmeticException,里面的值 error 表示異常后跳轉(zhuǎn)的視圖。

    對應(yīng)的,新建一個error.html頁:

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>error</title>
    </head>
    <body>
    出現(xiàn)錯誤
    </body>
    </html>

    接下來,造一個異常:

    @RequestMapping("/testExceptionHandler")
      public String testExceptionHandler() {
          System.out.println(1/0);
          return "success";
      }

    正常情況下這個處理器會跳轉(zhuǎn)到 success 頁,但是里面有個 1/0的異常,所以會按照配置跳轉(zhuǎn)到 error 頁。

    重新部署,測試一下,訪問http://localhost:8080/springmvc/testExceptionHandler:

    SpringMVC基于配置的異常處理器怎么用

    成功跳轉(zhuǎn)到 error 頁。

    儲存異常信息

    此外,還可以繼續(xù)屬性exceptionAttribute,設(shè)置一個key用來存放異常信息,默認(rèn)存在當(dāng)前的請求域中:

    <!--配置異常處理-->
      <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
          <property name="exceptionMappings">
              <props>
                  <prop key="java.lang.ArithmeticException">error</prop>
              </props>
          </property>
          <!--exceptionAttribute屬性設(shè)置一個屬性名,將出現(xiàn)的異常信息在請求域中進(jìn)行共享-->
          <property name="exceptionAttribute" value="ex"></property>
      </bean>

    那么在 error 頁中就可以使用到ex來獲取異常信息了。

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>error</title>
    </head>
    <body>
    出現(xiàn)錯誤
    <p th:text="${ex}"></p>
    </body>
    </html>

    重新部署,刷新下頁面:

    SpringMVC基于配置的異常處理器怎么用

    二、基于注解的異常處理

    springmvc 同樣也提供了一套注解,通過注解方式也可以實(shí)現(xiàn)上述的異常處理。

    新建一個控制器 ExceptionController:

    //@ControllerAdvice將當(dāng)前類標(biāo)識為異常處理的組件
    @ControllerAdvice
    public class ExceptionController {
        //@ExceptionHandler 用于設(shè)置所標(biāo)識方法處理的異常
        @ExceptionHandler(value = {ArithmeticException.class, NullPointerException.class})
        public String testException(Exception ex, Model model){
            // ex表示當(dāng)前請求處理中出現(xiàn)的異常對象,放到請求域中
            model.addAttribute("ex", ex);
            return "error";
        }
    }

    @ControllerAdvice將當(dāng)前類標(biāo)識為異常處理的組件。

    ex表示當(dāng)前請求處理中出現(xiàn)的異常對象,用Model放到請求域中。

    現(xiàn)在注釋掉配置文件里的處理器,重新部署下,刷新http://localhost:8080/springmvc/testExceptionHandler:

    SpringMVC基于配置的異常處理器怎么用

    依然可以。

    關(guān)于“SpringMVC基于配置的異常處理器怎么用”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“SpringMVC基于配置的異常處理器怎么用”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI