<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Spring `HandlerInterceptor`示例 > [https://howtodoinjava.com/spring-mvc/spring-intercepting-requests-using-handlerinterceptor-with-example/](https://howtodoinjava.com/spring-mvc/spring-intercepting-requests-using-handlerinterceptor-with-example/) 如您所知,servlet 過濾器可以在處理該 servlet 之前和之后對它們處理的每個 Web 請求進行預處理和后處理。 以類似的方式,您可以在 Spring Web 應用程序中使用`HandlerInterceptor`接口來對由 Spring MVC 處理器處理的 Web 請求進行預處理和后處理。 這些處理器通常用于處理返回/提交的模型屬性,并將它們傳遞給視圖/處理器。 ## `HandlerInterceptor`接口 處理器攔截器是在 Spring 的 Web 應用程序上下文中配置的,因此它們可以利用任何容器功能,并引用容器中聲明的任何 bean。 可以為特定的 URL 映射注冊處理器攔截器,因此它僅攔截映射到某些 URL 的請求。 每個處理器攔截器都必須實現`HandlerInterceptor`接口,該接口包含要實現的三種回調方法:`preHandle()`,`postHandle()`和`afterCompletion()`。 * `preHandle()`:在請求處理器處理請求之前。 * `postHandle()`:請求處理器處理完請求后。 它提供對返回的`ModelAndView`對象的訪問,因此您可以在其中操作模型屬性。 * `afterCompletion()`:在完成所有請求處理之后,即在渲染視圖之后。 ## `HandlerInterceptorAdapter`類 `HandlerInterceptor`接口的問題在于,無論是否需要,新類都必須實現所有三種方法。 為避免覆蓋,可以使用`HandlerInterceptorAdapter`類。 此類實現`HandlerInterceptor`并提供默認的空白實現。 因此,無論您想在處理器中覆蓋什么方法,都可以從`HandlerInterceptorAdapter`對其進行擴展。 ## `HandlerInterceptor`接口示例 我在這里重用為 [**Spring MVC hello world 應用程序**](https://howtodoinjava.com/spring/spring-mvc/spring-mvc-hello-world-example/) 創建的代碼。 我正在創建一個`CustomRequestHandler`類。 此類方法(即`preHandle()`,`postHandle()`和`afterCompletion()`)將按照上述討論進行調用。 ```java package com.howtodoinjava.demo.handlers; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; public class CustomRequestHandler extends HandlerInterceptorAdapter { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { long startTime = System.currentTimeMillis(); request.setAttribute("startTime", startTime); return true; } public void postHandle( HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { long startTime = (Long) request.getAttribute("startTime"); request.removeAttribute("startTime"); long endTime = System.currentTimeMillis(); modelAndView.addObject("totalTime", endTime - startTime); System.out.println("Request Prcessing Time :: " + (endTime - startTime)); } public void afterCompletion( HttpServletRequest request, HttpServletResponse response, Object handler, Exception exceptionIfAny) throws Exception { System.out.println("View Rendered !!"); } } ``` 現在,創建請求處理器后,必須將其聲明為 spring 配置文件,以便 spring 可以在適當的時候將請求傳遞給它。 處理器攔截器已注冊到`DefaultAnnotationHandlerMapping` bean,負責將攔截器應用于標有`@Controller`注解的任何類。 您可以在攔截器屬性中指定多個攔截器,其類型為數組。 ```java <bean id="customRequestHandler" class="com.howtodoinjava.demo.handlers.CustomRequestHandler" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <ref bean="customRequestHandler" /> </list> </property> </bean> ``` 現在,當您再次運行 hello world 應用程序時,它將在輸出下方顯示。 ``` Request Prcessing Time :: 15 View Rendered !! ``` ## 僅將`HandlerInterceptor`接口應用于某些 URL 好吧,使用上述方法,您的處理器將被應用到應用程序中的所有控制器上,而不管它們映射到什么 URL。 如果您只想將處理器映射到某些 URL,則必須使用`<mvc:interceptors>`標簽。 ```java <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/somepath_one/*"/> <ref bean="customRequestHandler_one" /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/somepath_two/*"/> <ref bean="customRequestHandler_two" /> </mvc:interceptor> </mvc:interceptors> ``` 這就是這個快速提示。 **祝您學習愉快!**
                  <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>

                              哎呀哎呀视频在线观看