springmvc interceptor拦截器 <mvc:mapping path="/**" />

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.classnull"****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

  1. 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 ");  
        }  
    }  

    ②YouController
    package 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失败

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值