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

歡迎訪問 生活随笔!

生活随笔

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

javascript

SpringMVC拦截器之拦截器接口方法演示

發布時間:2024/4/13 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringMVC拦截器之拦截器接口方法演示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>springmvc_day02_04_interceptor</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>springmvc_day02_04_interceptor Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><spring.version>5.0.2.RELEASE</spring.version></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.0</version><scope>provided</scope></dependency></dependencies> </project> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 開啟注解掃描 --><context:component-scan base-package="com.learn"/><!-- 視圖解析器對象 --><bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/pages/"/><property name="suffix" value=".jsp"/></bean><!--配置攔截器--><mvc:interceptors><!--配置攔截器--><mvc:interceptor><!--要攔截的具體的方法--><mvc:mapping path="/user/*"/><!--不要攔截的方法<mvc:exclude-mapping path=""/>--><!--配置攔截器對象--><bean class="com.learn.interceptor.MyInterceptor1" /></mvc:interceptor><!--配置第二個攔截器--><mvc:interceptor><!--要攔截的具體的方法--><mvc:mapping path="/**"/><!--不要攔截的方法<mvc:exclude-mapping path=""/>--><!--配置攔截器對象--><bean class="com.learn.interceptor.MyInterceptor2" /></mvc:interceptor></mvc:interceptors><!-- 開啟SpringMVC框架注解的支持 --><mvc:annotation-driven /></beans> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><h3>攔截器</h3><a href="user/testInterceptor" >攔截器</a></body> </html> package com.learn.controller;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;@Controller @RequestMapping("/user") public class UserController {@RequestMapping("/testInterceptor")public String testInterceptor(){System.out.println("testInterceptor執行了...");return "success";}} /** Copyright 2002-2017 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.springframework.web.servlet;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.springframework.lang.Nullable; import org.springframework.web.method.HandlerMethod;/*** Workflow interface that allows for customized handler execution chains.* Applications can register any number of existing or custom interceptors* for certain groups of handlers, to add common preprocessing behavior* without needing to modify each handler implementation.** <p>A HandlerInterceptor gets called before the appropriate HandlerAdapter* triggers the execution of the handler itself. This mechanism can be used* for a large field of preprocessing aspects, e.g. for authorization checks,* or common handler behavior like locale or theme changes. Its main purpose* is to allow for factoring out repetitive handler code.** <p>In an asynchronous processing scenario, the handler may be executed in a* separate thread while the main thread exits without rendering or invoking the* {@code postHandle} and {@code afterCompletion} callbacks. When concurrent* handler execution completes, the request is dispatched back in order to* proceed with rendering the model and all methods of this contract are invoked* again. For further options and details see* {@code org.springframework.web.servlet.AsyncHandlerInterceptor}** <p>Typically an interceptor chain is defined per HandlerMapping bean,* sharing its granularity. To be able to apply a certain interceptor chain* to a group of handlers, one needs to map the desired handlers via one* HandlerMapping bean. The interceptors themselves are defined as beans* in the application context, referenced by the mapping bean definition* via its "interceptors" property (in XML: a &lt;list&gt; of &lt;ref&gt;).** <p>HandlerInterceptor is basically similar to a Servlet Filter, but in* contrast to the latter it just allows custom pre-processing with the option* of prohibiting the execution of the handler itself, and custom post-processing.* Filters are more powerful, for example they allow for exchanging the request* and response objects that are handed down the chain. Note that a filter* gets configured in web.xml, a HandlerInterceptor in the application context.** <p>As a basic guideline, fine-grained handler-related preprocessing tasks are* candidates for HandlerInterceptor implementations, especially factored-out* common handler code and authorization checks. On the other hand, a Filter* is well-suited for request content and view content handling, like multipart* forms and GZIP compression. This typically shows when one needs to map the* filter to certain content types (e.g. images), or to all requests.** @author Juergen Hoeller* @since 20.06.2003* @see HandlerExecutionChain#getInterceptors* @see org.springframework.web.servlet.handler.HandlerInterceptorAdapter* @see org.springframework.web.servlet.handler.AbstractHandlerMapping#setInterceptors* @see org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor* @see org.springframework.web.servlet.i18n.LocaleChangeInterceptor* @see org.springframework.web.servlet.theme.ThemeChangeInterceptor* @see javax.servlet.Filter*/ public interface HandlerInterceptor {/*** Intercept the execution of a handler. Called after HandlerMapping determined* an appropriate handler object, but before HandlerAdapter invokes the handler.* <p>DispatcherServlet processes a handler in an execution chain, consisting* of any number of interceptors, with the handler itself at the end.* With this method, each interceptor can decide to abort the execution chain,* typically sending a HTTP error or writing a custom response.* <p><strong>Note:</strong> special considerations apply for asynchronous* request processing. For more details see* {@link org.springframework.web.servlet.AsyncHandlerInterceptor}.* <p>The default implementation returns {@code true}.* @param request current HTTP request* @param response current HTTP response* @param handler chosen handler to execute, for type and/or instance evaluation* @return {@code true} if the execution chain should proceed with the* next interceptor or the handler itself. Else, DispatcherServlet assumes* that this interceptor has already dealt with the response itself.* @throws Exception in case of errors*/default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {return true;}/*** Intercept the execution of a handler. Called after HandlerAdapter actually* invoked the handler, but before the DispatcherServlet renders the view.* Can expose additional model objects to the view via the given ModelAndView.* <p>DispatcherServlet processes a handler in an execution chain, consisting* of any number of interceptors, with the handler itself at the end.* With this method, each interceptor can post-process an execution,* getting applied in inverse order of the execution chain.* <p><strong>Note:</strong> special considerations apply for asynchronous* request processing. For more details see* {@link org.springframework.web.servlet.AsyncHandlerInterceptor}.* <p>The default implementation is empty.* @param request current HTTP request* @param response current HTTP response* @param handler handler (or {@link HandlerMethod}) that started asynchronous* execution, for type and/or instance examination* @param modelAndView the {@code ModelAndView} that the handler returned* (can also be {@code null})* @throws Exception in case of errors*/default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,@Nullable ModelAndView modelAndView) throws Exception {}/*** Callback after completion of request processing, that is, after rendering* the view. Will be called on any outcome of handler execution, thus allows* for proper resource cleanup.* <p>Note: Will only be called if this interceptor's {@code preHandle}* method has successfully completed and returned {@code true}!* <p>As with the {@code postHandle} method, the method will be invoked on each* interceptor in the chain in reverse order, so the first interceptor will be* the last to be invoked.* <p><strong>Note:</strong> special considerations apply for asynchronous* request processing. For more details see* {@link org.springframework.web.servlet.AsyncHandlerInterceptor}.* <p>The default implementation is empty.* @param request current HTTP request* @param response current HTTP response* @param handler handler (or {@link HandlerMethod}) that started asynchronous* execution, for type and/or instance examination* @param ex exception thrown on handler execution, if any* @throws Exception in case of errors*/default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,@Nullable Exception ex) throws Exception {}} package com.learn.interceptor;import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/*** 自定義攔截器*/ public class MyInterceptor1 implements HandlerInterceptor{/*** 預處理,controller方法執行前* return true 放行,執行下一個攔截器,如果沒有,執行controller中的方法* return false不放行* @param request* @param response* @param handler* @return* @throws Exception*/public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("MyInterceptor1執行了...前1111");// request.getRequestDispatcher("/WEB-INF/pages/error.jsp").forward(request,response);return true;}/*** 后處理方法,controller方法執行后,success.jsp執行之前* @param request* @param response* @param handler* @param modelAndView* @throws Exception*/public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("MyInterceptor1執行了...后1111");// request.getRequestDispatcher("/WEB-INF/pages/error.jsp").forward(request,response);}/*** success.jsp頁面執行后,該方法會執行* @param request* @param response* @param handler* @param ex* @throws Exception*/public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("MyInterceptor1執行了...最后1111");}} package com.learn.interceptor;import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/*** 自定義攔截器*/ public class MyInterceptor2 implements HandlerInterceptor{/*** 預處理,controller方法執行前* return true 放行,執行下一個攔截器,如果沒有,執行controller中的方法* return false不放行* @param request* @param response* @param handler* @return* @throws Exception*/public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("MyInterceptor2執行了...前2222");// request.getRequestDispatcher("/WEB-INF/pages/error.jsp").forward(request,response);return true;}/*** 后處理方法,controller方法執行后,success.jsp執行之前* @param request* @param response* @param handler* @param modelAndView* @throws Exception*/public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("MyInterceptor2執行了...后2222");// request.getRequestDispatcher("/WEB-INF/pages/error.jsp").forward(request,response);}/*** success.jsp頁面執行后,該方法會執行* @param request* @param response* @param handler* @param ex* @throws Exception*/public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("MyInterceptor2執行了...最后2222");}} <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><h3>錯誤頁面</h3></body> </html> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><h3>執行成功</h3><% System.out.println("success.jsp執行了..."); %></body> </html>

?

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的SpringMVC拦截器之拦截器接口方法演示的全部內容,希望文章能夠幫你解決所遇到的問題。

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