1.继承抽象类HandlerInterceptorAdapter
package com.kp.spring.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.kp.spring.util.SystemUtils;
/**
* @author: py
* @version:2016年10月17日 上午11:28:43
* com.kp.spring.interceptor.TestInterceptor.java
* @Desc SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求
* 并进行相应的处理。比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那样子判断当前时间是
* 否是购票时间。
*/
public class TestInterceptor extends HandlerInterceptorAdapter {
/**
* preHandle方法是进行处理器拦截用的,顾名思义,该方法将在Controller处理之前进行调用,
* SpringMVC中的Interceptor拦截器是链式的,可以同时存在 多个Interceptor,
* 然后SpringMVC会根据声明的前后顺序一个接一个的执行,而且所有的Interceptor中的preHandle方法都会在
* Controller方法调用之前调用。
* SpringMVC的这种Interceptor链式结构也是可以进行中断的,这种中断方式是令preHandle的返
* 回值为false,
* 当preHandle的返回值为false的时候整个请求就结束了。
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
SystemUtils.println(TestInterceptor.class, null, "****1preHandle");
String path = request.getServletPath();
String contextPath = request.getContextPath() ;
SystemUtils.println(TestInterceptor.class, "path", path);
SystemUtils.println(TestInterceptor.class, "contextPath", contextPath);
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
if(handlerMethod.getMethodAnnotation(ResponseBody.class)!=null){
//ajax 请求不拦截
return true;
} else{
....
}
}
}
2.声明Controller
①MyController
-
package com.kp.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@RequestMapping("my.do")
public void my(){
System.out.println("test my ");
}
}
②YouControllerpackage com.kp.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("you")
public class YouController {
@RequestMapping("you.do")
public void you(){
System.out.println("test you ");
}
}
3.配置文件如果是
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<bean class="com.kp.spring.interceptor.TestInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
浏览器输入:http://localhost:8080/SpringMvcKP/my.do
控制台输出:
时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****1preHandle
test my
时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****2postHandle
时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****3afterCompletion
浏览器输入:http://localhost:8080/SpringMvcKP/you/you.do
控制台输出:test you
结果:
成功拦截http://localhost:8080/SpringMvcKP/my.do
拦截http://localhost:8080/SpringMvcKP/you/you.do失败
4.配置文件如果是
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.kp.spring.interceptor.TestInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>浏览器输入:http://localhost:8080/SpringMvcKP/my.do
控制台输出:
时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****1preHandle
test my
时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****2postHandle
时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****3afterCompletion
浏览器输入:http://localhost:8080/SpringMvcKP/you/you.do
控制台输出:
时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****1preHandle
test you
时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****2postHandle
时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****3afterCompletion
结果:
成功拦截http://localhost:8080/SpringMvcKP/you/you.do
成功拦截http://localhost:8080/SpringMvcKP/my.do
5.配置文件如果是
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/you/*"/>
<bean class="com.kp.spring.interceptor.TestInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>浏览器输入:http://localhost:8080/SpringMvcKP/my.do
控制台输出:test my
浏览器输入:http://localhost:8080/SpringMvcKP/you/you.do
控制台输出:
时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****1preHandle
test you
时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****2postHandle
时间:2016-10-22 17:10:46;类:com.kp.spring.interceptor.TestInterceptor;内容:****3afterCompletion
结果:
拦截http://localhost:8080/SpringMvcKP/my.do失败
成功拦截http://localhost:8080/SpringMvcKP/you/you.do
6.配置文件如果是
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/"/>
<bean class="com.kp.spring.interceptor.TestInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
浏览器输入:http://localhost:8080/SpringMvcKP/my.do
控制台输出:test my
浏览器输入:http://localhost:8080/SpringMvcKP/you/you.do
控制台输出:test you
结果:
拦截http://localhost:8080/SpringMvcKP/my.do失败
拦截http://localhost:8080/SpringMvcKP/you/you.do失败