<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Spring MVC `SimpleMappingExceptionResolver`示例 > [https://howtodoinjava.com/spring-mvc/spring-mvc-simplemappingexceptionresolver-example/](https://howtodoinjava.com/spring-mvc/spring-mvc-simplemappingexceptionresolver-example/) 在某些編碼錯誤的應用程序中,當發生未知異常時,應用程序服務器通常會在網頁本身中向用戶顯示惡意異常堆棧跟蹤。 在這種情況下,用戶與此堆棧跟蹤無關,并抱怨您的應用程序對用戶不友好。 此外,當您向用戶公開內部方法調用層次結構時,它還可能證明存在潛在的安全風險。 盡管可以將 Web 應用程序的`web.xml`配置為在發生 HTTP 錯誤或類異常的情況下顯示友好的 JSP 頁面,但是 Spring MVC 支持一種更強大的方法來管理類異常的視圖。 ## `HandlerExceptionResolver`和`SimpleMappingExceptionResolver` 在 Spring MVC 應用程序中,可以在 Web 應用程序上下文中注冊一個或多個異常解析器 bean,以解析未捕獲的異常。 這些 Bean 必須實現`DispatcherServlet`的`HandlerExceptionResolver`接口才能自動檢測它們。 Spring MVC 附帶了一個簡單的異常解析器,即`SimpleMappingExceptionResolver`,用于以可配置的方式將每個類別的異常映射到一個視圖。 假設我們有一個異常類,即`AuthException`。 而且,我們希望每次將此異常從任何地方拋出到應用程序中時,我們都希望顯示一個預定的視圖頁面`/WEB-INF/views/error/authExceptionView.jsp`。 這樣配置就可以了。 **`SimpleMappingExceptionResolver`配置** ```java <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="com.howtodoinjava.demo.exception.AuthException"> error/authExceptionView </prop> </props> </property> <property name="defaultErrorView" value="error/genericView"/> </bean> ``` 完整的上下文配置為: `applicationContext.xml` ```java <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.howtodoinjava.demo" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="messages" /> </bean> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="com.howtodoinjava.demo.exception.AuthException"> error/authExceptionView </prop> </props> </property> <property name="defaultErrorView" value="error/genericView"/> </bean> </beans> ``` 請注意最后的`defaultErrorView`屬性。 如果 spring 上下文檢測到未從應用程序拋出的任何異常,但未在`exceptionMappings`屬性列表中列出,則它將呈現視圖`/WEB-INF/views/error/genericView.jsp`。 ## 測試`SimpleMappingExceptionResolver`配置的應用程序 為了測試目的,讓我們創建`AuthException.java`。 `AuthException.java` ```java package com.howtodoinjava.demo.exception; import java.util.Date; public class AuthException extends RuntimeException { private static final long serialVersionUID = 1L; private Date date; private String message; public AuthException(Date date, String message) { super(); this.date = date; this.message = message; } public Date getDate() { return date; } public String getMessage() { return message; } @Override public String toString() { return "AuthException [date=" + date + ", message=" + message + "]"; } } ``` 并從任何控制器拋出此異常。 `EmployeeController.java` ```java @Controller @RequestMapping("/employee-module") public class EmployeeController { @RequestMapping(value="/getAllEmployees", method = RequestMethod.GET) public String welcome(Model model) { throw new AuthException(new Date(), "Something bad happened dude !! Run Away :-("); } } ``` 并在路徑`/WEB-INF/views/error/`中創建兩個 jsp 文件 `authExceptionView.jsp` ```java <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>Authentication Exception</title> </head> <body> <h2>Exception occured at: </h2><fmt:formatDate value="${exception.date}" pattern="yyyy-MM-dd" /> <h2>Exception Message : </h2>${exception.message} </body> </html> ``` `genericView.jsp` ```java <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <html> <head> <title>Generic Exception</title> </head> <body> <h2>Some generic error message</h2> </body> </html> ``` **現在點擊 URL:`http://localhost:8080/springmvcexample/employee-module/getAllEmployees`** ![SimpleMappingExceptionResolver Example](https://img.kancloud.cn/3e/e2/3ee28c0838b4c39fe8c74d8957085932_569x294.jpg) `SimpleMappingExceptionResolver`示例 **現在從控制器拋出任何其他異常**,例如`NullPointerException`,如下所示。 ```java @Controller @RequestMapping("/employee-module") public class EmployeeController { @RequestMapping(value="/getAllEmployees", method = RequestMethod.GET) public String welcome(Model model) { throw new NullPointerException(); } } ``` **然后再次點擊以下網址:`http://localhost:8080/springmvcexample/employee-module/getAllEmployees`** ![SimpleMappingExceptionResolver Generic Error Example](https://img.kancloud.cn/40/b0/40b08a301fd283d8254598e13f55378c_537x205.jpg) `SimpleMappingExceptionResolver` 通用錯誤示例 顯然,應用程序現在可以在出現異常的情況下找到正確的視圖。 前視圖中沒有更多錯誤堆棧跟蹤。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看