区块链技术博客
www.b2bchain.cn

22.SpringMVC拦截器(interceptor)

这篇文章主要介绍了22.SpringMVC拦截器(interceptor)的讲解,通过具体代码实例进行16250 讲解,并且分析了22.SpringMVC拦截器(interceptor)的详细步骤与相关技巧,需要的朋友可以参考下https://www.b2bchain.cn/?p=16250

本文实例讲述了2、树莓派设置连接WiFi,开启VNC等等的讲解。分享给大家供大家参考文章查询地址https://www.b2bchain.cn/7039.html。具体如下:

目录

 

一、Interceptor作用

二、拦截器和过滤器的区别

三、拦截器的一些说明


一、Interceptor作用

拦截器类似于Servlet中的过滤器Filter,用于对处理器进行预处理和后处理

将拦截器按照一定的顺序连成一条链,这条链成为拦截器链(Interceptor Chain)。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按照之前定义好的顺序调用,拦截器也是AOP思想的实现。

 

二、拦截器和过滤器的区别

(1)适用范围:

过滤器是servlet里的,任何java web都可以使用。

拦截器是springmvc里的,只有springmvc框架才可以使用

(2)拦截范围

过滤器:在url-pattern中配置了/*之后,可以对所有要访问的资源拦截

拦截器:只会拦截访问的控制器方法,如果访问的是jsp,html,css,image,js,是不会拦截的

 

三、拦截器实例

(1)如何自定义拦截器

*1)创建拦截器实现HandlerInterceptor接口

*2)配置拦截器

*3)测试拦截器的拦截效果

 

(2)实例

目录

22.SpringMVC拦截器(interceptor)

pom.xml

<?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.xupeng</groupId>   <artifactId>interceptor</artifactId>   <version>1.0-SNAPSHOT</version>   <packaging>war</packaging>    <name>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.7</maven.compiler.source>     <maven.compiler.target>1.7</maven.compiler.target>   </properties>    <dependencies>     <dependency>       <groupId>org.springframework</groupId>       <artifactId>spring-context</artifactId>       <version>5.0.5.RELEASE</version>     </dependency>     <dependency>       <groupId>junit</groupId>       <artifactId>junit</artifactId>       <version>4.12</version>     </dependency>     <dependency>       <groupId>mysql</groupId>       <artifactId>mysql-connector-java</artifactId>       <version>8.0.16</version>     </dependency>      <dependency>       <groupId>c3p0</groupId>       <artifactId>c3p0</artifactId>       <version>0.9.1.2</version>     </dependency>      <dependency>       <groupId>javax.servlet</groupId>       <artifactId>javax.servlet-api</artifactId>       <version>3.0.1</version>     </dependency>      <dependency>       <groupId>javax.servlet.jsp</groupId>       <artifactId>javax.servlet.jsp-api</artifactId>       <version>2.2.1</version>     </dependency>      <dependency>       <groupId>org.springframework</groupId>       <artifactId>spring-web</artifactId>       <version>5.0.5.RELEASE</version>     </dependency>      <dependency>       <groupId>org.springframework</groupId>       <artifactId>spring-webmvc</artifactId>       <version>5.0.5.RELEASE</version>     </dependency>      <dependency>       <groupId>com.fasterxml.jackson.core</groupId>       <artifactId>jackson-core</artifactId>       <version>2.9.0</version>     </dependency>      <dependency>       <groupId>com.fasterxml.jackson.core</groupId>       <artifactId>jackson-databind</artifactId>       <version>2.9.0</version>     </dependency>      <dependency>       <groupId>com.alibaba</groupId>       <artifactId>fastjson</artifactId>       <version>1.2.71</version>     </dependency>      <dependency>       <groupId>commons-fileupload</groupId>       <artifactId>commons-fileupload</artifactId>       <version>1.3.1</version>     </dependency>      <dependency>       <groupId>commons-io</groupId>       <artifactId>commons-io</artifactId>       <version>2.3</version>     </dependency>    </dependencies>    <build>     <finalName>interceptor</finalName>     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->       <plugins>         <plugin>           <artifactId>maven-clean-plugin</artifactId>           <version>3.1.0</version>         </plugin>         <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->         <plugin>           <artifactId>maven-resources-plugin</artifactId>           <version>3.0.2</version>         </plugin>         <plugin>           <artifactId>maven-compiler-plugin</artifactId>           <version>3.8.0</version>         </plugin>         <plugin>           <artifactId>maven-surefire-plugin</artifactId>           <version>2.22.1</version>         </plugin>         <plugin>           <artifactId>maven-war-plugin</artifactId>           <version>3.2.2</version>         </plugin>         <plugin>           <artifactId>maven-install-plugin</artifactId>           <version>2.5.2</version>         </plugin>         <plugin>           <artifactId>maven-deploy-plugin</artifactId>           <version>2.8.2</version>         </plugin>       </plugins>     </pluginManagement>   </build> </project> 

web.xml

<!DOCTYPE web-app PUBLIC  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  "http://java.sun.com/dtd/web-app_2_3.dtd" >  <web-app>   <display-name>Archetype Created Web Application</display-name>    <!--配置SpringMVC的前端控制器-->   <servlet>     <servlet-name>DispatcherServlet</servlet-name>     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>     <init-param>       <param-name>contextConfigLocation</param-name>       <param-value>classpath:spring-mvc.xml</param-value>     </init-param>     <load-on-startup>1</load-on-startup>   </servlet>   <servlet-mapping>     <servlet-name>DispatcherServlet</servlet-name>     <url-pattern>/</url-pattern>   </servlet-mapping> </web-app> 

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xmlns:mvc="http://www.springframework.org/schema/mvc"        xmlns:context="http://www.springframework.org/schema/context"        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">      <context:component-scan base-package="com.xupeng.controller"></context:component-scan>      <!--配置内部资源视图解析器-->     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">         <property name="prefix" value="/"></property>         <property name="suffix" value=".jsp"></property>     </bean>      <!--mvc的注解驱动-->     <mvc:annotation-driven></mvc:annotation-driven>      <!--配置文件上传解析器-->     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">         <property name="defaultEncoding" value="UTF-8"/>         <property name="maxUploadSize" value="5000000"/>     </bean>      <!--静态资源访问-->     <mvc:default-servlet-handler></mvc:default-servlet-handler>  <mvc:interceptors>         <mvc:interceptor>             <mvc:mapping path="/**"/>             <bean class="com.xupeng.interceptor.MyInterceptor"></bean>         </mvc:interceptor>     </mvc:interceptors> </beans>

TargetController

package com.xupeng.controller;  import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;  @Controller public class TargetController {      @RequestMapping("/target")     public ModelAndView show(){         System.out.println("执行目标资源");         ModelAndView modelAndView = new ModelAndView();         modelAndView.addObject("name","xupeng");         modelAndView.setViewName("index");         return modelAndView;     } } 

MyInterceptor

package com.xupeng.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 MyInterceptor implements HandlerInterceptor {      @Override     //在目标方法执行之前执行     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {         System.out.println("preHandle");         return true;     }      @Override     //在目标方法执行之后,视图对象返回之前执行     public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {         System.out.println("postHandle");     }      @Override     //在所有流程都执行完毕后执行     public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {         System.out.println("afterCompletion");     } } 

结果:

22.SpringMVC拦截器(interceptor)

22.SpringMVC拦截器(interceptor)

 

三、拦截器的一些说明

(1)顺序

执行顺序看上述结果已经知道了,preHandle>目标方法>postHandle>afterCompletion

(2)多个拦截器的话,执行顺序咋样?

如果我们配置如下两个拦截器:

22.SpringMVC拦截器(interceptor)

输入结果为:

22.SpringMVC拦截器(interceptor)

输出结果和拦截器设置的顺序有关

本文转自互联网,侵权联系删除22.SpringMVC拦截器(interceptor)

赞(0) 打赏
部分文章转自网络,侵权联系删除b2bchain区块链学习技术社区 » 22.SpringMVC拦截器(interceptor)
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

b2b链

联系我们联系我们