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--的,这里把它设置为UTF-,解决有乱码的情况 -->
- <property name="supportedMediaTypes">
- <list>
- <value>text/html;charset=UTF-</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甚础上,起来越多的用到了注解,从前的拦截器在配置文件中需要这样配置 <beans...> ... <bean id="measurementInter ...
- springMVC拦截器简单配置
<!-- 拦截器 --> <mvc:interceptors> <mvc:interceptor> <!-- 拦截所 ...
- [转]SpringMVC拦截器简单教程
亲测有用,地址: http://blog.csdn.net/tjcyjd/article/details/7498236
- springmvc拦截器的简单了解
1.定义一个拦截器 2.在springmvc.xml中配置拦截器. (1)拦截器拦截的请求是建立在前端控制器配置之下的,若DispatcherServlet拦截的是*.action,则拦截器即使配置 ...
- SpringMVC拦截器的使用
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- SpringMVC拦截器(实现登录验证拦截器)
本例实现登陆时的验证拦截,采用SpringMVC拦截器来实现 当用户点击到网站主页时要进行拦截,用户登录了才能进入网站主页,否则进入登陆页面 核心代码 首先是index.jsp,显示链接 <%@ ...
- SpringMVC拦截器Interceptor
SpringMVC拦截器(Interceptor)实现对每一个请求处理前后进行相关的业务处理,类似与servlet中的Filter. SpringMVC 中的Interceptor 拦截请求是通过Ha ...
- 五 : springMVC拦截器
springMVC拦截器的实现一般有两种方式 第一种方式是要定义的Interceptor类要实现了Spring的HandlerInterceptor 接口 第二种方式是继承实现了HandlerInte ...
- SpringMVC 拦截器实现原理和登录实现
SpringMVC 拦截器的原理图 springMVC拦截器的实现一般有两种方式 第一种方式是要定义的Interceptor类要实现了Spring的HandlerInterceptor 接口 第二种方 ...
随机推荐
- python学习笔记——多进程中共享内存Value & Array
1 共享内存 基本特点: (1)共享内存是一种最为高效的进程间通信方式,进程可以直接读写内存,而不需要任何数据的拷贝. (2)为了在多个进程间交换信息,内核专门留出了一块内存区,可以由需要访问的进程将 ...
- Python学习笔记014——迭代工具函数 内置函数enumerate()
1 描述 enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中. 2 语法 enumerate(sequ ...
- Python学习笔记010——匿名函数lambda
1 语法 my_lambda = lambda arg1, arg2 : arg1 + arg2 + 1 arg1.arg2:参数 arg1 + arg2 + 1 :表达式 2 描述 匿名函数不需要r ...
- Spring.net(一)----Spring.NET框架简介及模块说明
简介: Spring.NET框架包括很多功能,Spring.NET 1.0包括完整的IoC容器和AOP类库.1.1版加入Web.ORM和数据模块.Spring.NET的下载包中并不包含与其它类库 ...
- Linux中断 - IRQ number和中断描述符
一.前言 本文主要围绕IRQ number和中断描述符(interrupt descriptor)这两个概念描述通用中断处理过程.第二章主要描述基本概念,包括什么是IRQ number,什么是中断描述 ...
- php model与json_encode/json_decode
常用于model的操作,看看就知道了 <?php class UserModel { var $user_id = 0; var $user_name = ''; var $user_email ...
- python科学计算基础知识
1.导入基本函数库 import numpy as np 2.获取矩阵元素字节数 a=np.array([1,2,3],dtype=np.float32) a.itemsizeoutput: 4 3. ...
- 【Android】2.1 PhonewordApp—第1个Android应用程序
分类:C#.Android.VS2015: 创建日期:2016-02-04 本例子演示如何添加一个简单的单页导航,在此基础上,再演示如何在第2个页面中显示第1个页面中拨打过的所有电话号码. (1)通 ...
- CCNotificationCenter(一)
const std::string testsName[MAX_COUNT] = { "Bug-350", "Bug-422", "Bug-458&q ...
- C++范围解析运算符::的使用
1.范围解析运算符的作用范围解析运算符 :: 用于标识不同范围内使用的标识符. 2.范围解析运算符的使用1)用于命名空间和类 namespace NamespaceA{ int x; class Cl ...