SpringMVC拦截器的配置和使用

dispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

1

dispatcherServlet

/

spring-core.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=“http://www.springframework.org/schema/beans”

xmlns:context=“http://www.springframework.org/schema/context”

xmlns:aop=“http://www.springframework.org/schema/aop”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation="

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

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

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

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

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

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

<context:component-scan base-package=“com.gxwz.code” />

<context:component-scan base-package=“com.gxwz.core” />

spring-mvc.xml (关键配置文件)

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=“http://www.springframework.or 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 g/schema/beans”

xmlns:context=“http://www.springframework.org/schema/context”

xmlns:mvc=“http://www.springframework.org/schema/mvc”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation="

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

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

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

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

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

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

<context:component-scan base-package=“com.gxwz.web” />

<mvc:annotation-driven />

mvc:interceptors

mvc:interceptor

<mvc:mapping path=“/**”/>

</mvc:interceptor>

</mvc:interceptors>

IndexInterceptor.java

package com.gxwz.interceptor;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**

  • 在业务处理器处理请求之前被调用

  • 如果返回false

  • 从当前的拦截器往回执行所有拦截器的afterCompletion(),再退出拦截器链

  • 如果返回true

  • 执行下一个拦截器,直到所有的拦截器都执行完毕

  • 再执行被拦截的Controller

  • 然后进入拦截器链,

  • 从最后一个拦截器往回执行所有的postHandle()

  • 接着再从最后一个拦截器往回执行所有的afterCompletion()

  • 与过滤器的区别

  • 1.过滤器是依赖于Servlet容器,基于回调函数,Interceptor依赖与框架,基于反射机制

  • 2.过滤器的过滤范围更大,还可以过滤一些静态资源,拦截器只拦截请求

*/

public class IndexInterceptor extends HandlerInterceptorAdapter {

@Override

public boolean preHandle(HttpServletRequest request,HttpServletResponse response,

Object handler) throws Exception {

//业务逻辑代码编写…(如:解决乱码,权限验证)

System.out.println(“preHandle(), 在访问Controller之前被调用”);

return true;

}

/**

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值