SpringMVC12拦截器
创建对应的登陆界面
<body>
<form action="login" method="post">
姓名: <input type="text" name="userName"/>
<input type="submit" value="新增"/>
</form>
</body>
创建对应的controller
@Controller
public class LoginController { /**
* 默认用户登录时候的方法
*/
@RequestMapping("/login")
public String addStudent(String userName,HttpSession session) {
System.out.println("进入了 login");
session.setAttribute("name", userName);
return "/user/list"; //转发
} }
@Controller
@RequestMapping("/user")
public class UserController { @RequestMapping("/list")
public ModelAndView list(){
System.out.println("进入了List");
ModelAndView mv=new ModelAndView();
mv.addObject("name", "大家辛苦了!").setViewName("/success.jsp");
return mv;
}
}
创建对应的拦截器
public class MyInterceptor implements HandlerInterceptor {
/**
* 在处理器映射器 映射出所要执行的处理器类时,已经将拦截器和处理器组合成一个处理器执行链!并返回给了中央调度器!
*
* preHandle:预处理
* request:请求
* response:响应
* handler:即将执行的controller
*
* 01.preHandle return是false的时候 不会执行后续的操作 只有一个拦截器的时候
* 02.有两个拦截器 而且两个preHandle都是return===false
* 只输出第一个拦截器中的语句!后续不会执行
* 03.有两个拦截器 第一个拦截器中preHandle的return===true
* 执行的方法:
* 001.第1个拦截器的preHandle
* 002.第2个拦截器的preHandle
* 003.第1个拦截器的afterCompletion
* 004.没有执行controller
* 04.有两个拦截器 两个拦截器中preHandle的return===true
* 001.第1个拦截器的preHandle
* 002.第2个拦截器的preHandle
* 003.执行controller
* 004.第2个拦截器的postHandle
* 005.第1个拦截器的postHandle
* 006.第2个拦截器的afterCompletion
* 007.第1个拦截器的afterCompletion
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
System.out.println("进入了 MyInterceptor1的 preHandle=====》");
return true;
}
/**
* Controller执行之后的方法
* 如果有多个拦截器
* 那么 执行顺序和 perHandle相反!
*
* ModelAndView:就是从controller中返回的数据!
* 我们可以操作!
*/
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("进入了 MyInterceptor1的 postHandle=====》");
System.out.println(modelAndView.getViewName());
}
/**
* afterCompletion:后处理
* 想要执行必须满足一个条件! 本类中的perHandle 执行并且返回true!
*/
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("进入了 MyInterceptor1的 afterCompletion=====》");
}
}
public class MyInterceptor2 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
System.out.println("进入了 MyInterceptor2的 preHandle=====》");
return true;
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("进入了 MyInterceptor2的 postHandle=====》");
}
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("进入了 MyInterceptor2的 afterCompletion=====》");
}
}
在xml文件中配置
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 配置springmvc的组件 -->
<context:component-scan base-package="cn.bdqn.controller"/>
<!-- 开启 注解的方式-->
<mvc:annotation-driven/> <!-- 配置拦截器 拦截 user/下面的请求-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/user/**"/>
<bean class="cn.bdqn.interceptor.MyInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/user/**"/>
<bean class="cn.bdqn.interceptor.MyInterceptor2"/>
</mvc:interceptor>
</mvc:interceptors> </beans>

SpringMVC12拦截器的更多相关文章
- 6. ModelDriven拦截器、Preparable 拦截器
1. 问题 Struts2 的 Action 我们将它定义为一个控制器,但是由于在 Action 中也可以来编写一些业务逻辑,也有人会在 Action 输入业务逻辑层. 但是在企业开发中,我们一般会将 ...
- springmvc的拦截器
什么是拦截器 java里的拦截器是动态拦截action调用的对象.它提供了一种机制可以使 ...
- Struts的拦截器
Struts的拦截器 1.什么是拦截器 Struts的拦截器和Servlet过滤器类似,在执行Action的execute方法之前,Struts会首先执行Struts.xml中引用的拦截器,在执行完所 ...
- Struts2拦截器的执行过程浅析
在学习Struts2的过程中对拦截器和动作类的执行过程一度陷入误区,特别读了一下Struts2的源码,将自己的收获分享给正在困惑的童鞋... 开始先上图: 从Struts2的图可以看出当浏览器发出请求 ...
- 学习SpringMVC——拦截器
拦截器,顾名思义就是用来拦截的. 那什么是拦截,又为什么要拦截.对于Spring MVC来说,拦截器主要的工作对象就是用户的请求,拦截下来之后,我们可以在拦截的各个阶段悉心呵护[为所欲为].常见的比如 ...
- alias拦截器的使用
在SSH项目中,有时需要由一个Action跳转到另一个Action.有两种方式可以实现Action之间的跳转,一种是chain,另一种是redirectAction,这两种方式之间的区别是chain是 ...
- Struts2入门(二)——配置拦截器
一.前言 之前便了解过,Struts 2的核心控制器是一个Filter过滤器,负责拦截所有的用户请求,当用户请求发送过来时,会去检测struts.xml是否存在这个action,如果存在,服务器便会自 ...
- 通过拦截器Interceptor实现Spring MVC中Controller接口访问信息的记录
java web工程项目使用了Spring+Spring MVC+Hibernate的结构,在Controller中的方法都是用于处理前端的访问信息,Controller通过调用Service进行业务 ...
- spring 拦截器
1.mvc.xml <!-- 自定义拦截链配置 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping p ...
随机推荐
- 我学C的那些年[ch01]:浅淡C语言的编译过程
前几天大致学习了C语言的编译过程,那么今天就和大家分享一下 首先,编译C语言,需要一个文本编辑器(windows自带的也行),和一个MinGW编译器(需要配置环境),就可以将.c文件编译成.exe文件 ...
- Github Https方式push错误”Empty reply from server”
1 2 3 4 5 6 7 8 9 10 2014-11-19 20:41:30.130 GitHub for Mac Login[2595:326257] AskPass ...
- android开发学习笔记:圆角的Button
转自:http://www.cnblogs.com/gzggyy/archive/2013/05/17/3083218.html 在res目录下的drawable-mdpi建立xml文件shape.x ...
- iscc2016-basic-find-to-me
额 第一题就暴力搜索了 已知仿射加密变换为c=(11m+8)mod26,试对密文sjoyuxzr解密 #include <stdio.h> int main(void) { int m,c ...
- Expert Shell Scripting
Expert Shell Scripting 好好学习这本书
- 图片的css自适应
当需要css来缩放图片的时候,可以采用外层容器100%或者任意百分比, 内层图片img tag 没有宽高,用sass写经过断点后的mixin中的样式就是这样: .workscon_section{ w ...
- Unity3d Web3d资源的动态加载
Unity3d Web3d资源的动态加载 @灰太龙 参考了宣雨松的博客,原文出处http://www.xuanyusong.com/archives/2405,如果涉及到侵权,请通知我! Unity3 ...
- 一篇故事讲述了计算机网络里的基本概念:网关,DHCP,IP寻址,ARP欺骗,路由,DDOS等...
计算机主机网关的作用是什么? 假设你的名字叫小不点,你住在一个大院子里,你的邻居有很多小伙伴,在门口传达室还有个看大门的李大爷,李大爷就是你的网关.当你想跟院子里的某个小伙伴玩,只要你在院子里大喊一声 ...
- android 随手记 广播通知栏 二
关于通知栏的使用: Notification及NotificationManager的使用详解 相关类: import android.app.NotificationManager; import ...
- c++实现委托
#include "stdafx.h" #include <iostream> #include <string> using namespace std; ...