SpringMVC拦截器简单使用
一、拦截器的配置
1、传统的配置
- <bean
- class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >
- <property name="interceptors">
- <!-- 多个拦截器,顺序执行 -->
- <list>
- <ref bean="commonInterceptor"/>
- </list>
- </property>
- </bean>
- <!--
- 如果不定义mappingURL,则默认拦截所有对Controller的请求 ;
- 可以使用正则表达式对url进行匹配,从而更细粒度的进行拦截(.*/entryOrJsonController\.do\?action=reg.*);
- -->
- <bean id="commonInterceptor" class="com.wy.interceptor.CommonInterceptor">
- <property name="mappingURL" value=".*/entryOrJsonController\.do\?action=reg.*"/>
- </bean>
2、基于注解的配置
- <!-- 拦截器 -->
- <mvc:interceptors>
- <!-- 多个拦截器,顺序执行 -->
- <mvc:interceptor>
- <mvc:mapping path="/entryOrJsonController
- @Override
- public boolean preHandle(HttpServletRequest request,
- HttpServletResponse response, Object handler) throws Exception {
- // TODO Auto-generated method stub
- log.info("==============执行顺序: 1、preHandle================");
- String url=request.getRequestURL().toString();
- if(mappingURL==null || url.matches(mappingURL)){
- request.getRequestDispatcher("/msg.jsp").forward(request, response);
- return false;
- }
- return true;
- }
- //在业务处理器处理请求执行完成后,生成视图之前执行的动作
- @Override
- public void postHandle(HttpServletRequest request,
- HttpServletResponse response, Object handler,
- ModelAndView modelAndView) throws Exception {
- // TODO Auto-generated method stub
- log.info("==============执行顺序: 2、postHandle================");
- }
- @Override
- public void afterCompletion(HttpServletRequest request,
- HttpServletResponse response, Object handler, Exception ex)
- throws Exception {
- // TODO Auto-generated method stub
- log.info("==============执行顺序: 3、afterCompletion================");
- }
- }
注意:注释中的说明。
完整的spring配置文件
- <?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:mvc="http://www.springframework.org/schema/mvc"
- xmlns:util="http://www.springframework.org/schema/util"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.springframework.org/schema/util
- http://www.springframework.org/schema/util/spring-util-3.0.xsd">
- <!-- 默认的注解映射的支持 ,它会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter
- <mvc:annotation-driven />
- -->
- <!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd -->
- <mvc:resources mapping="/jsentryOrJsonController\.do\?action=reg.*);
- -->
- <bean id="commonInterceptor" class="com.wy.interceptor.CommonInterceptor">
- <property name="mappingURL" value=".*/entryOrJsonController\.do\?action=reg.*"/>
- </bean>
- <!-- 处理方法级别上的@RequestMapping注解-->
- <bean
- class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
- <property name="messageConverters">
- <util:list id="beanList">
- <ref bean="mappingJacksonHttpMessageConverter"/>
- </util:list>
- </property>
- </bean>
- <!--
- 将指定路径的请求直接转到对应的view上,而不需要特定的controller来处理请求 .
- 注意:此处的映射路径是/hello,请求时http://localhost:8080/SpringMVC/hello
- 不能在hello.xxx,而不使用此种方式的映射可以加的,因为web.xml配置的是‘/’
- -->
- <mvc:view-controller path="/hello" view-name="hello" />
- <!-- 视图解析器策略 和 视图解析器 -->
- <!-- 对JSTL提供良好的支持 -->
- <bean
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <!-- 默认的viewClass,可以不用配置
- <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" />
- -->
- <property name="prefix" value="/WEB-INF/page/" />
- <property name="suffix" value=".jsp" />
- </bean>
- <!-- 处理JSON数据转换的 -->
- <bean id="mappingJacksonHttpMessageConverter"
- class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
- <!-- 为了处理返回的JSON数据的编码,默认是ISO-88859-1的,这里把它设置为UTF-8,解决有乱码的情况 -->
- <property name="supportedMediaTypes">
- <list>
- <value>text/html;charset=UTF-8</value>
- </list>
- </property>
- </bean>
- <!-- 拦截器 -->
- <mvc:interceptors>
- <!-- 多个拦截器,顺序执行 -->
- <mvc:interceptor>
- <mvc:mapping path="/entryOrJsonController/*" /><!-- 如果不配置或/*,将拦截所有的Controller -->
- <bean class="com.wy.interceptor.CommonInterceptor"></bean>
- </mvc:interceptor>
- </mvc:interceptors>
- <!--
- ResourceBundleViewResolver通过basename所指定的ResourceBundle解析视图名。
- 对每个待解析的视图,ResourceBundle里的[视图名].class所对应的值就是实现该视图的类。
- 同样,[视图名].url所对应的值是该视图所对应的URL。
- 可以指定一个parent view,其它的视图都可以从parent view扩展。
- 用这种方法,可以声明一个默认的视图。
- <bean id="messageSource"
- class="org.springframework.context.support.ResourceBundleMessageSource">
- <property name="basename" value="welcome" />
- </bean>
- -->
- </beans>
SpringMVC拦截器简单使用相关推荐
- springMVC 拦截器简单配置
在spring 3.0甚础上,起来越多的用到了注解,从前的拦截器在配置文件中需要这样配置 [html] view plaincopy <beans...> ... <bean id= ...
- SpringMVC拦截器HandlerInterceptor原理及使用
在使用SpringMVC拦截器的时候,我们接触的最多的便是HandlerInterceptor接口,因为我们所有的自定义拦截器都必须要实现HandlerInterceptor接口,那么就先从Handl ...
- SpringMVC拦截器Interceptor
SpringMVC拦截器(Interceptor)实现对每一个请求处理前后进行相关的业务处理,类似与servlet中的Filter. SpringMVC 中的Interceptor 拦截请求是通过Ha ...
- SpringMVC拦截器
一.SpringMVC拦截器 1.简单的拦截器开发 1.添加依赖 需要注意的是<!--为了避免与Tomcat自带的servleAPI冲突,只有在开发编译才会使用,发布时会被排除在外--> ...
- SpringMVC拦截器HandlerInterceptor拦截后返回数据或视图View
SpringBoot版本:2.1.6.RELEASE SpringMVC版本:5.1.8.RELEASE SpringMVC拦截器 比如说在SpringMVC Web环境下,需要实现一个权限拦截的功能 ...
- springMVC 拦截器
为什么80%的码农都做不了架构师?>>> 实现springMVC 拦截器步骤: 1.定义拦截器类××××HandlerInterceptor 继承HandlerInterce ...
- springmvc 拦截器、国际化、验证
2019独角兽企业重金招聘Python工程师标准>>> springmvc 拦截器 继承了HandlerIntercepter的类可以作为拦截器类: package com.yawn ...
- Spring+SpringMVC+MyBatis深入学习及搭建(十七)——SpringMVC拦截器
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7098753.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十六)--S ...
- SpringMVC拦截器与异常处理
SpringMVC拦截器与异常处理 参考文章: (1)SpringMVC拦截器与异常处理 (2)https://www.cnblogs.com/luckgood/p/9724057.html 备忘一下 ...
最新文章
- linux下c语言俄罗斯方块,C语言实现俄罗斯方块源代码
- 【Android 逆向】ART 脱壳 ( DexClassLoader 脱壳 | exec_utils.cc 中执行 Dex 编译为 Oat 文件的 Exec 和 ExecAndReturnC函数 )
- 我常用的python函数(part2)--executemany方法
- grade项目导入新external libraries_【易推荐】德意志银行学院中国项目启动
- python怎么把列表转换成字符串_如何将字节列表(unicode)转换为Python字符串?
- loop设备及losetup命令介绍[转]
- 聊聊LiteOS事件模块的结构体、初始化及常用操作
- ORA-01552 :非系统表空间 'xxxx'不能使用系统回退段
- sap未分摊差异怎么处理_MM采购中形成的差异
- git如何下载代码:
- PDF页面旋转怎么操作
- “踢群第一案”上热搜的背后
- d : 无法将“d”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
- 用计算机控制食品生产论文,计算机控制系统论文范文2篇
- VMV-GCN Volumetric Multi-View Based Graph论文分析
- 分享一组制作游戏用的人物立绘/角色形象素材图片,共182张图片
- LOSS加权重解决数据imbalance
- MyCobot六轴机械臂的基本操作(二)
- 社会实践(二)暑期计划
- OpenDDS内部关键的idl文件(InfoRepoDiscovery)
热门文章
- Nacos 1.3.0 发布, 全新内核构建
- linux后台执行命令:amp;与nohup的用法
- Spring Boot 第三篇:SpringBoot用JdbcTemplates访问Mysql
- 图解Transformer:Attention Is All You Need
- 0基础玩转CV的利器绝了,还送万元礼金,机不可失!
- 多模态深度学习:用深度学习的方式融合各种信息
- 在抖音推荐算法组工作的体验
- 2020 年 6 月编程语言排行榜,C 稳居第一,Rust 首进 Top 20!
- 阿里达摩院新研究:实习生一作突破自动驾驶技术难题,首次实现3D物体检测精度与速度兼得...
- 一文带你了解卷积神经网络基础,建议收藏