java中自定义拦截器_SpringMVC自定义拦截器实现过程详解

SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。开发者可以自定义一些拦截器来实现特点的功能。

过滤器与拦截器的区别:拦截器是AOP思想的具体应用。

过滤器

servlet规范中的一部分,任何java web工程都可以使用

在url-pattern中配置了/*之后,可以对所有要访问的资源进行拦截

拦截器

拦截是SpringMVC框架自己的,只要使用SpringMVC框架的工程才能使用

拦截器只会拦截访问的控制方法,如果访问的是jsp/html/css/image/js是不会进行拦截的

自定义拦截器

想要自定义拦截器,必须实现HandlerInterceptor接口

1、新建一个Moudule ,Sprringmvc-07-Interceptor,添加web支持

2、配置 web.xml 和 springmvc-servlet.xml 文件

3、编写一个拦截器

web.xml

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

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:applicationContext.xml

1

SpringMVC

/

encoding

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

encoding

/*

springmvc-servlet.xml

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">

id="internalResourceViewResolver">

MyInterceptor.java

package com.min.config;

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 {

// return true; 放行会执行下一个拦截器, 放行

//return false; 不执行下一个拦截器

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

System.out.println("============处理前============");

return true;

}

/**

//日志

public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

System.out.println("============处理后============");

}

public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

System.out.println("============清理============");

}

*/

}

TestController.java

package com.min.controller;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class TestController {

@GetMapping("/t1")

public String test() {

System.out.println("TestController=>test()执行了");

return "ok";

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值