mvc设计模式之拦截器的开发

MVC设计模式之拦截器的开发

1.新建一个普通的java类,继承HandlerInterceptor接口

2.实现接口中的三个方法

1.preHandle():在controller控制器之前执行

2.postHandle():在controller控制器之后,在响应页面之前执行

3.afterCompletion():在响应完页面之后执行

3.拦截器的运行过程

拦截器的运行过程:

1.客户端发起请求,进入拦截器,运行所有拦截器的preHandle方法,当preHandle方法返回

false时,从当前拦截器往回执行所有拦截器的afterCompletion方法,在退出拦截器链

2.当preHandle方法全为true时,执行下一个拦截器,直到所有的拦截器执行完,在运行被拦截

的Controller,然后进入拦截器链,运行所有拦截器的postHandler方法,

完成从最后一个拦截 器往回执行所有拦截器的afterCompletion方法

3.当preHandle返回true时,映射处理器执行链将继续执行,当返回false时,

DispatcherServlet处理器认为拦截器已经处理完了请求,而不继续执行拦截器中的其他拦截器和处理器

4.spring拦截器的主要作用

1.解决乱码问题:用request和response参数设置编码

2.解决权限验证问题:是否登陆,获取session查看

3.日志的处理等等

5.拦截器的配置(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:context="http://www.springframework.org/schema/context"

xmlns:jdbc="http://www.springframework.org/schema/jdbc"

xmlns:jee="http://www.springframework.org/schema/jee"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:util="http://www.springframework.org/schema/util"

xmlns:jpa="http://www.springframework.org/schema/data/jpa"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/jdbc

http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd

http://www.springframework.org/schema/jee

http://www.springframework.org/schema/jee/spring-jee-3.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

http://www.springframework.org/schema/data/jpa

http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util-3.2.xsd">

 

<mvc:interceptors>

<mvc:i6nterceptor>

<!-- path表示拦截路径,如果是拦截所有写/**

exclude-mapping 表示不拦截的请求

mapping 表示拦截所有

 -->

<mvc:mapping path="/user/*"/>

<!-- exclude-mapping 表示不拦截的URL请求    只有mapper的表示的是拦截-->

<mvc:mapping path="/user/*"/>

<mvc:exclude-mapping path="/user/showLogin.do"/>

<!-- 实例化拦截器对象 -->

<bean class="cn.tedu.store.interceptor.LoginInteceptor"/>

</mvc:interceptor>

</mvc:interceptors>

6.过滤器和拦截器的区别

1.过滤器依赖于Servlet容器,而拦截器依赖于框架容器(如spring)

2.过滤器基于函数回调实现,拦截器基于java反射原理

3.过滤器可以过滤任何的请求,拦截器只能针对action的请求

4.拦截器可以访问action上下文,堆栈里面的对象,过滤器不能

5.拦截器可以获取ioc容器中的各个bean,过滤器不能

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值