SSM-SpringMVC-33:SpringMVC中拦截器Interceptor讲解

释放双眼,带上耳机,听听看~!

 

 

** ————吾亦无他,唯手熟尔,谦卑若愚,好学若饥————-**

 

 

 

拦截器Interceptor:

对处理方法进行双向的拦截,可以对其做日志记录等

 

我选择的是实现HandlerInterceptor接口

 

我把执行流程放上来,单个拦截器的和多个拦截器的,然后写个Demo案例,就可以正式结束Springmvc,过几天写完SSM整合,SSM就结束了,将来有机会多放几个项目在博客园上

 

图1,单个拦截器执行流程

SSM-SpringMVC-33:SpringMVC中拦截器Interceptor讲解

 

图2,多个拦截器的执行流程

SSM-SpringMVC-33:SpringMVC中拦截器Interceptor讲解

 

 

 

我会给大家定义俩个拦截器,大家跑一下,看看执行顺序,另外大家可以举一反三的扩充,定义多个拦截器或者在拦截器的方法中填实际的处理代码

 

案例Demo开始

1.自定义第一个拦截器FirstInterceptor

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1package cn.dawn.day25interceptor;/**
2 * Created by dawn on 2018-04-02.
3 */
4
5import org.springframework.web.servlet.HandlerInterceptor;
6import org.springframework.web.servlet.ModelAndView;
7
8import javax.servlet.http.HttpServletRequest;
9import javax.servlet.http.HttpServletResponse;
10
11public class FirstInterceptor implements HandlerInterceptor{
12    //在Handler执行前执行
13    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
14        System.out.println("preHandle=============");
15        return true;
16    }
17
18    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
19        System.out.println("postHandle=============");
20    }
21
22    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
23        System.out.println("afterCompletion=============");
24    }
25}
26

 

 

 

preHandle这个方法,它的返回值决定了下面俩个方法是否执行,如果false,则不会执行下面的那俩方法

 

2.自定义第二个拦截器SecondInterceptor

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1package cn.dawn.day25interceptor;/**
2 * Created by dawn on 2018-04-02.
3 */
4
5import org.springframework.web.servlet.HandlerInterceptor;
6import org.springframework.web.servlet.ModelAndView;
7
8import javax.servlet.http.HttpServletRequest;
9import javax.servlet.http.HttpServletResponse;
10
11
12public class SecondInterceptor implements HandlerInterceptor {
13    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
14        System.out.println("Second preHandle=============");
15        return true;
16    }
17
18    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
19        System.out.println("Second postHandle=============");
20    }
21
22    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
23        System.out.println("Second afterCompletion=============");
24    }
25}
26

 

 

 

3.自定义处理器和处理方法

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1package cn.dawn.day25interceptor;/**
2 * Created by dawn on 2018-04-02.
3 */
4
5import org.springframework.stereotype.Controller;
6import org.springframework.web.bind.annotation.RequestMapping;
7
8@Controller
9public class FirstController {
10    @RequestMapping("/first")
11    public String doFirst(){
12        System.out.println("Handler method");
13        return "success";
14    }
15}
16

 

 

 

4.自己的xml配置文件中配置一道,将拦截器配置上去

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4       xmlns:mvc="http://www.springframework.org/schema/mvc"
5       xmlns:context="http://www.springframework.org/schema/context"
6       xsi:schemaLocation="http://www.springframework.org/schema/beans
7        http://www.springframework.org/schema/beans/spring-beans.xsd
8        http://www.springframework.org/schema/mvc
9        http://www.springframework.org/schema/mvc/spring-mvc.xsd
10         http://www.springframework.org/schema/context
11         http://www.springframework.org/schema/context/spring-context.xsd">
12
13    <!--包扫描器-->
14    <context:component-scan base-package="cn.dawn.day25interceptor"></context:component-scan>
15    <!--视图解析器-->
16    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17        <property name="prefix" value="/day25/"></property>
18        <property name="suffix" value=".jsp"></property>
19    </bean>
20
21    <!--拦截器配置-->
22    <mvc:interceptors>  <!--配置多个拦截器,多个拦截器链-->
23
24        <mvc:interceptor>
25            <mvc:mapping path="/**"/>
26            <bean class="cn.dawn.day25interceptor.FirstInterceptor"></bean>
27        </mvc:interceptor>
28        <mvc:interceptor>
29            <mvc:mapping path="/**"/>
30            <bean class="cn.dawn.day25interceptor.SecondInterceptor"></bean>
31        </mvc:interceptor>
32
33    </mvc:interceptors>
34
35    <!--绑定注解驱动-->
36    <mvc:annotation-driven></mvc:annotation-driven>
37
38</beans>
39

 

 

 

**此处的拦截器位置的前后,决定了他们执行的先后
**

此处的/**表示任意多级目录的任意方法,全部的意思

 

5.修改web.xml的中央调度器的上下文配置位置为上面那个xml

 

6.jsp页面,我不是return了个success么,所以建一个success.jsp

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1<%--
2  Created by IntelliJ IDEA.
3  User: Dawn
4  Date: 2018/4/2
5  Time: 14:19
6  To change this template use File | Settings | File Templates.
7--%>
8<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
9<html>
10<head>
11    <title>成功</title>
12</head>
13<body>
14    <h1>SUCCESS</h1>
15</body>
16</html>
17

 

 

 

7.启动tomcat,访问处理器的那个处理方法/first

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C/C++内存泄漏及检测

2022-1-11 12:36:11

安全运维

如何防止黑客从“漏洞”偷走你的年终奖 | 年末必看

2016-12-19 1:15:19

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索