zoukankan      html  css  js  c++  java
  • spring mvc拦截器interceptor

    1.  SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理。比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那样子判断当前时间是否是购票时间。

    2.HandlerInterceptor接口-》SpringMVC 中的Interceptor ->import org.springframework.web.servlet.HandlerInterceptor;  

    3.ModelAndView->import org.springframework.web.servlet.HandlerInterceptor

    第一种方式是要定义的Interceptor类要实现了Spring 的HandlerInterceptor 接口,或者是这个类继承实现了HandlerInterceptor 接口的类,比如Spring 已经提供的实现了HandlerInterceptor 接口的抽象类HandlerInterceptorAdapter ;第二种方式是实现Spring的WebRequestInterceptor接口,或者是继承实现了WebRequestInterceptor的类。

    4.

    1. import javax.servlet.http.HttpServletRequest;  
    2. import javax.servlet.http.HttpServletResponse;  
    3.   
    4. import org.springframework.web.servlet.HandlerInterceptor;  
    5. import org.springframework.web.servlet.ModelAndView;  
    6.   
    7. public class SpringMVCInterceptor implements HandlerInterceptor { 

    5.返回值为false,当preHandle的返回值为false的时候整个请求就结束了。 

    1.  * 回值为false,当preHandle的返回值为false的时候整个请求就结束了。 
    2.      */  
    3.     @Override  
    4.     public boolean preHandle(HttpServletRequest request,  
    5.             HttpServletResponse response, Object handler) throws Exception {  
    6.         // TODO Auto-generated method stub  
    7.         return false;  
    8.     }  

    6.postHandle()多了一个modelandview参数,很好理解,controller处理完,肯定要返回modelandview嘛

    prehandle()的参数就没有modelandview,也很好理解,进来http的时候,是不知道controller要返回的modelandview嘛,还没进入controller

    1. @Override  
    2.     public void postHandle(HttpServletRequest request,  
    3.             HttpServletResponse response, Object handler,  
    4.             ModelAndView modelAndView) throws Exception {  
    5.         // TODO Auto-generated method stub  
    6.           
    7.     }  
  • 相关阅读:
    [转]UNI-APP开发笔记之使用uni.navigateBack修改上一个页面值
    [转]移动端人员选择的设计思考
    [转]nginx安装及其配置详细教程
    [转]Vue 使用use、prototype自定义自己的全局组件
    [转]uniapp项目运行支付宝小程序,报错:xxx.json中没有申明component: true
    支付宝(钉钉)小程序使用uView控制台报错Cannot read property 'title-all' of undefined
    [转]commonJS规范和require,import区别
    [转]module.exports和export详解
    [转]如何在组件中去使用vuex的值和方法?
    [转]CryptoJS中AES256(CBC)加密算法简单使用
  • 原文地址:https://www.cnblogs.com/panxuejun/p/6733220.html
Copyright © 2011-2022 走看看