目录
包结构
坐标引入
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.7</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
mvc配置文件springmvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--组件扫描 主要扫描controller-->
<context:component-scan base-package="demo.controller" />
<!--启用spring的一些annotation -->
<context:annotation-config/>
<!--配置mvc注解驱动-->
<mvc:annotation-driven/>
<!--SpringMVC提供的通用资源放行方式-->
<mvc:default-servlet-handler/>
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 声明拦截器 -->
<mvc:interceptors>
<!-- 声明第一个拦截器 -->
<mvc:interceptor>
<!--
指定拦截器的拦截地址
path: 拦截器的uri地址,使用 ** 通配符
-->
<mvc:mapping path="/**"/>
<!-- 指定使用的拦截器 -->
<bean class="demo.interceptor.MyInterceptor" />
</mvc:interceptor>
<!-- 声明第二个拦截器 -->
<mvc:interceptor>
<!--
指定拦截器的拦截地址
path: 拦截器的uri地址,使用 ** 通配符
-->
<mvc:mapping path="/**"/>
<!-- 指定使用的拦截器 -->
<bean class="demo.interceptor.MyInterceptor2" />
</mvc:interceptor>
<!-- 声明第二个拦截器 -->
<mvc:interceptor>
<!--
指定拦截器的拦截地址
path: 拦截器的uri地址,使用 ** 通配符
-->
<mvc:mapping path="/**"/>
<!-- 指定使用的拦截器 -->
<bean class="demo.interceptor.MyInterceptor3" />
</mvc:interceptor>
</mvc:interceptors>
</beans>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 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:springmvc.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>
<!--乱码过滤器-->
<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>
</web-app>
控制器
package demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@RequestMapping("/index")
public String getIndex(){
System.out.println("index....");
return "index";
}
}
拦截器
package demo.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 {
//preHandle在控制前执行
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle 前置拦截111");
//返回true后才可以执行控制器方法
return true;
}
//postHandle在控制后执行
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle 后置拦截111");
}
//afterCompletion最终执行
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion 最终拦截111");
}
}
多拦截器执行顺序:
正常执行顺序:
当拦截器2前置拦截返回false:
当拦截器3前置拦截返回false: