SpringMvc中想使用拦截器,主要分为两步:

a、编写拦截器,需实现HandlerInterceptor接口

b、springmvc.xml中配置拦截器

逻辑图如下:

0027SpringMVC拦截器的编写和配置-LMLPHP

测试过程主要分为如下几步:

1、编写interceptorIndex.jsp

2、编写InterceptorController.java

3、编写interceptorSuccess.jsp

4、编写拦截器MyInterceptor1.java

5、springmvc.xml中配置拦截器

具体实现如下:

1、编写interceptorIndex.jsp

<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/12/8
Time: 17:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>SpringMVC Interceptor</title>
</head>
<body>
<h3>SpringMVC Interceptor测试</h3>
<a href="interceptor/interceptorTest">interceptor</a>
</body>
</html>

2、编写InterceptorController.java

package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/interceptor")
public class InterceptorController {
public static final String SUCCESS = "interceptorSuccess";
@RequestMapping("interceptorTest")
public String interceptorTest(){
System.out.println("执行controller中的方法");
return SUCCESS;
}
}

3、编写interceptorSuccess.jsp

<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/12/8
Time: 17:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>执行成功</h3>
<%System.out.println("执行interceptorSuccess.jsp"); %>
</body>
</html>

4、编写拦截器MyInterceptor1.java

package com.example.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 {
@Override
/*1、返回true代表放行,可以处理后续的拦截器或者controller中的方法
* 2、返回false代表后续的内容不会被执行,可用参数中的request或者response跳转到错误页面或者登录页面等(权限验证)*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("执行MyInterceptor1的前置方法");
return true;
} @Override
/*结果页面执行完之后执行*/
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("执行MyInterceptor1的后置方法");
} @Override
  
/*也可使用参数的request或者response跳转到某些页面,则controller中的return不会被执行,因此可以控制不会跳两个页面*/
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("执行MyInterceptor1的afterCompletion方法");
}
}

5、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:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">
<!--开启注解扫描-->
<context:component-scan base-package="com.example" />
<!--视图解析器,根据Controller返回的字符串找对应的文件-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--文件路径-->
<property name="prefix" value="/WEB-INF/pages/" />
<!--文件后缀-->
<property name="suffix" value=".jsp" />
</bean>
<!--配置自定义类型转换器-->
<bean id="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.example.util.StingToDateConvertr" />
</set>
</property>
</bean> <!--1、开启springmvc框架注解的支持-->
<!--2、欲使配置的自定义类型转换器生效,需加上conversion-service属性-->
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/> <!--jsp页面引入的js文件也会被拦截,配置哪些静态资源文件不要被前端控制器拦截-->
<mvc:resources mapping="/js/" location="/js/**" />
<mvc:resources mapping="/css/" location="/css/**" /> <!--SpringMVC文件上传需配置文件解析器,且要求id必须是multipartResolver,
否则controller的方法中的参数是接收不到值的,会报空指针-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760" />
</bean> <!--配置拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<!--配置拦截哪些方法-->
<mvc:mapping path="/interceptor/*"/>
<!--配置不拦截哪些方法-->
<!--<mvc:exclude-mapping path=""/>-->
<!--使用的拦截器类-->
<bean class="com.example.interceptor.MyInterceptor1"/>
</mvc:interceptor>
<mvc:interceptor>
<!--配置拦截哪些方法-->
<mvc:mapping path="/interceptor/*"/>
<!--配置不拦截哪些方法-->
<!--<mvc:exclude-mapping path=""/>-->
<!--使用的拦截器类-->
<bean class="com.example.interceptor.MyInterceptor2"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>

MyInterceptor2.java与MyInterceptor1.java代码基本相同,不重复罗列。

测试:启动项目,在interceptorIndex.jsp点击链接访问controller中的方法,执行结果如下:

0027SpringMVC拦截器的编写和配置-LMLPHP

结果总结:

1、执行拦截器的前置方法

2、执行controller中的方法

3、执行拦截器的后置方法

4、执行controller中的return,进入相应的页面,执行页面中的代码

5、页面执行完之后,执行拦截的afterCompletion方法。

若有理解不到之处,望指正!

05-24 08:52