
一、拦截器简介
拦截器(Interceptor)是一种动态拦截方法调用的机制
作用:
1.在指定的方法调用前后执行预先设定后的的代码
2.阻止原始方法的执行
核心原理:AoP思想
·拦截器链:多个拦截器按照一定的顺序,对原始被调用功能进行增强
三、自定义拦截器开发过程(指功能不够时自己开发,springmvc有拦截器的)
1、拦截器开发
制作拦截器功能类(通知)
配置拦截器的执行位置(切入点)
1.1拦截器开发入门
实现接口
在springMVC的配置里配置
案例、
结构、
环境、
1、pom.xml
- <!-- servlet规范-->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.1.0</version>
- <scope>provided</scope>
- </dependency>
- <!-- jsp -->
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.1</version>
- <scope>provided</scope>
- </dependency>
-
- <!-- spring-->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>5.1.9.RELEASE</version>
- </dependency>
- <!-- spring Mvc-->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>5.1.9.RELEASE</version>
- </dependency>
- <!-- spring Web-->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>5.1.9.RELEASE</version>
- </dependency>
2、web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- version="3.0">
- <!--用于解决中文乱码-->
- <filter>
- <filter-name>CharacterEncodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!--拦截-->
- <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>
- </servlet>
- <servlet-mapping>
- <servlet-name>DispatcherServlet</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
-
- </web-app>
3、controller层的方法
4、实现拦截接口
5、spring-mvc.xml
6、测试路径:http://localhost:8080/handleRun
四、拦截器配置与拦截器方法参数
1、拦截器参数
1.1、
1.2、
1.3、
2、拦截器配置项
评价
排名
4
文章
473
粉丝
3
评论
2
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术