package org.springframework.web.servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** 拦截器接口,内部就3个方法 **/ public interface HandlerInterceptor { /**[调用前] controller执行前调用此方法 返回true表示继续执行,返回false中止执行 这里可以加入登录校验、权限拦截等 **/ boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception; /**[调用后,但还未渲染视图] controller执行后但未返回视图前调用此方法 这里可在返回用户前对模型数据进行加工处理,比如这里加入公用信息以便页面显示 **/ void postHandle( HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception; /**[调用后,已经渲染视图] controller执行后且视图返回后调用此方法 这里可得到执行controller时的异常信息 这里可记录操作日志,资源清理等 **/ void afterCompletion( HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception; }好的,那么下面我们尝试写个类来实现它,先把简单的拦截器先用起来。我们再来研究配置,下面增加了一些文件。
package com.billstudy.springmvc.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; /** * 自定义拦截器01 * @author Bill * @since V1.0 2015/02/04 */ public class MyInterceptor01 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("MyInterceptor01.preHandle()"); return true;<span style="white-space:pre"> </span>// 这里返回true,则请求可以到Controller中 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("MyInterceptor01.postHandle()"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("MyInterceptor01.afterCompletion()"); } }
package com.billstudy.springmvc.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; /** * 通过请求本Controller内部方法,结合拦截器测试效果 * @author Bill * @since V1.0 2015/02/04 */ @Controller @RequestMapping("/demo") public class DemoController { @RequestMapping("/demo01") public ModelAndView demo01(HttpServletRequest request,HttpServletResponse response){ System.out.println("DemoController.demo01()"); ModelAndView result = new ModelAndView("/demo"); result.addObject("msg", "我是萌哒哒的msg"); return result; } }
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>demo page</title> </head> <body> msg:${msg} </body> </html>
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd "> <!-- auto scanner class path --> <context:component-scan base-package="com.billstudy.springmvc" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/pages" /> <property name="suffix" value=".jsp" /> </bean> <mvc:interceptors> <mvc:interceptor> <!-- path="/user/*.do" /user/所有请求 path="/*/*" 任意两级请求 path="*" 无效 path="/**" 任意请求 --> <mvc:mapping path="/**"/> <bean class="com.billstudy.springmvc.interceptor.MyInterceptor01" /> </mvc:interceptor> </mvc:interceptors> </beans>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> <property name="<span style="color:#ff0000;">interceptors</span>"> <list> <bean id="handlerInterceptor1" class="springmvc.intercapter.HandlerInterceptor1" /> <bean id="handlerInterceptor2" class="springmvc.intercapter.HandlerInterceptor2" /> <span style="color:#ff0000;">// ... 可以配置多个,按配置的顺序调用</span> </list> </property> </bean>配置全局的拦截器就是:
<mvc:interceptors> <mvc:interceptor> <!-- path="/user/*.do" /user/所有请求 path="/*/*" 任意两级请求 path="*" 无效 path="/**" 任意请求 --> <mvc:mapping path="/**"/> <bean class="com.billstudy.springmvc.interceptor.MyInterceptor01" /> </mvc:interceptor> <span style="color:#ff0000;"><!-- 这里也可以配置多个 mvc:interceptor 标签 --></span> </mvc:interceptors>
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.billstudy.springmvc.interceptor.MyInterceptor01" /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.billstudy.springmvc.interceptor.MyInterceptor02" /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.billstudy.springmvc.interceptor.MyInterceptor03" /> </mvc:interceptor> <!-- 这里也可以配置多个 mvc:interceptor 标签 --> </mvc:interceptors>
package com.billstudy.springmvc.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; /** * 自定义拦截器02 * @author Bill * @since V1.0 2015/02/04 */ public class MyInterceptor02 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("MyInterceptor02.preHandle()"); <strong><span style="color:#cc0000;">return false; // 这里返回false,请求在这里被截断!</span></strong> } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("MyInterceptor02.postHandle()"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("MyInterceptor02.afterCompletion()"); } }
原文地址:http://blog.csdn.net/u010811257/article/details/43492437