拦截器的方法介绍(三)
2016-01-19 16:51
274 查看
1.preHandle 方法,在请求被处理之前进行调用。
2.postHandle 方法,在请求被处理之后进行调用。
3.affterCompletion 方法,在请求结束之后才进行调用。
<span style="font-size:18px;">package com.lee.springmvc.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/***
* 设置拦截器(在 applicationContext-config.xml 中配置 注册到配置文件中)
* @author liyintao
*
*/
public class Test1Interceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("执行到了afterCompletion方法!");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
System.out.println("执行到了postHandle方法!");
//可以通过ModelAndView参数来改变现实的视图,或修改发放视图的方法。
arg3.addObject("errorMsg", "通过拦截器修改消息!");
}
//返回值:表示我们是否需要将房钱的请求拦截下来
//如果返回false,请求将被终止!不会到达controller中,也不会调用另外两个方法
//如果返回true,请求会被继续运行
//Object arg2 表示的是被拦截的请求的目标对象--(UsersController.java中的userList方法)
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2) throws Exception {
System.out.println("执行到了preHandle方法!");
return true;//暂时先改为true
}
}
</span>
2.postHandle 方法,在请求被处理之后进行调用。
3.affterCompletion 方法,在请求结束之后才进行调用。
<span style="font-size:18px;">package com.lee.springmvc.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/***
* 设置拦截器(在 applicationContext-config.xml 中配置 注册到配置文件中)
* @author liyintao
*
*/
public class Test1Interceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("执行到了afterCompletion方法!");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
System.out.println("执行到了postHandle方法!");
//可以通过ModelAndView参数来改变现实的视图,或修改发放视图的方法。
arg3.addObject("errorMsg", "通过拦截器修改消息!");
}
//返回值:表示我们是否需要将房钱的请求拦截下来
//如果返回false,请求将被终止!不会到达controller中,也不会调用另外两个方法
//如果返回true,请求会被继续运行
//Object arg2 表示的是被拦截的请求的目标对象--(UsersController.java中的userList方法)
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2) throws Exception {
System.out.println("执行到了preHandle方法!");
return true;//暂时先改为true
}
}
</span>
相关文章推荐
- ios已上架的APP更新,使用core data的需要升级core Data数据库,这样做
- 15 个必知的 Android 开发者选项
- 检查Mac电脑是32位还是64位运行以及修改方法
- C++经典面试题
- ERROR 1820 (HY000): You must SET PASSWORD before executing this
- 安卓APP动态调试技术
- 正态分布的生成
- DFRobot的产品
- iOS:删除已经配置的类库和移除CocoaPods
- 1月19号 TextField
- MVC缓存OutPutCache学习笔记 (三) 缓存局部刷新
- linux 如何查看防火墙是否开启
- 所有类的超类:Object类
- 很好文章
- 视频采集处理之YUV数据格式
- ImageTarget识别物体脱离识别案例
- git概念、原理、使用
- Node定义
- Java内存模型FAQ(二) 其他语言,像C++,也有内存模型吗?
- jQuery的各种插入方法