spring之拦截器
拦截器
实现HandlerInterceptor接口:注册拦截器<mvc:inteceptors>
spring和springMVC父子容器的关系
在spring整体框架的核心概念中,容器是核心思想,就是用来管理Bean的整个生命周期的,而在一个项目中,容器不一定只有一个,spring中可以包括多个容器,而且容器有上下层关系,目前最常见的一种场景就是在项目中引入spring和springmvc两个框架,那么它其实就是两个容器,spring是父容器,springmvc是子容器,并且在spring父容器中注册的bean对于springmvc容器中是可见的,而在springmvc容器中注册的bean对于spring父容器中就是不可见的,也就是子容器可以看见父容器中注册的Bean,反之就不行。
全部拦截:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/springmvc/hello" method="post">
姓名:<input type="text" name="username"><br>
年龄:<input type="text" name="age"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
package com.bjsxt.handlers; import java.util.Map; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping; //后台控制器
@Controller
@Scope("prototype")
@RequestMapping("/springmvc")
public class MyController{
//接收json字符串封装成对象
@RequestMapping("/hello")
public String hello1(String username,int age,Model model,Map<String,Object>map,ModelMap modelMap){
System.out.println(username+"______"+age);
model.addAttribute("username", username);
System.out.println("MyController.hello1()");
map.put("age", age);
modelMap.addAttribute("gender", "female");
System.out.println("MyController.hello1()");
return "welcome"; } }
package com.bjsxt.interceptors; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
//自定义拦截器
public class FistInterceptor implements HandlerInterceptor {
//该方法执行时机:处理器方法执行之前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
System.out.println("拦截器执行");
return false;
} //该方法执行时机:所有方法处理完成执行之后,响应给浏览器客户端执行
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub } //该方法执行时机:处理器方法执行之后
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
// TODO Auto-generated method stub } }
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注册组件扫描器 -->
<context:component-scan base-package="com.bjsxt.handlers"></context:component-scan>
<!-- 注册注解驱动 -->
<mvc:annotation-driven/>
<!-- 注册视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 注册拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean id="" class="com.bjsxt.interceptors.FistInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- 静态资源无法访问第二种解决方案 -->
<!-- <mvc:default-servlet-handler/> -->
<!-- 静态资源无法访问第三种解决方案 -->
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
</beans>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
欢迎页面!${username}--${age}--${gender}
</body>
</html>
指定拦截
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/springmvc/hello2" method="POST">
姓名:<input type="text" name="username"><br/>
年龄:<input type="text" name="age"><br/>
<input type="submit" value="提交"><br/>
</form>
</body>
</html>
package com.bjsxt.handlers; import java.util.Map; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping; //后端控制器
@Controller
@Scope("prototype")
@RequestMapping("/springmvc")
public class MyController{
@RequestMapping("/hello")
public String hello1(String username,int age,Model model,Map<String, Object> map,ModelMap modelMap){
System.out.println(username + " ----------"+age);
model.addAttribute("username", username);
map.put("age", age);
modelMap.addAttribute("gender", "female");
return "welcome";
}
@RequestMapping("/hello2")
public String hello2(String username,int age,Model model,Map<String, Object> map,ModelMap modelMap){
System.out.println(username + " 2222----------2222"+age);
model.addAttribute("username", username);
map.put("age", age);
modelMap.addAttribute("gender", "female");
return "welcome";
} }
package com.bjsxt.interceptors; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; //自定义拦截器
public class FirstInterceptor implements HandlerInterceptor {
//该方法执行时机:处理器方法执行之前执行
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("拦截器preHandle()执行!");
return true;
} //该方法执行时机:处理器方法执行之后执行
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("拦截器postHandle()执行!"); } //该方法执行时机:所有工作处理完成之后,响应给浏览器客户端之前执行
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("拦截器afterCompletion()执行!"); } }
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注册组件扫描器 -->
<context:component-scan base-package="com.bjsxt.handlers"></context:component-scan>
<!-- 注册注解驱动 -->
<mvc:annotation-driven/>
<!-- 注册视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <!-- 注册拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/springmvc/hello"/>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/springmvc/hello2"/>
<bean id="" class="com.bjsxt.interceptors.FirstInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors> <!-- 静态资源无法访问第二种解决方案 -->
<!-- <mvc:default-servlet-handler/> -->
<!-- 静态资源无法访问第三种解决方案 -->
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
</beans>
spring之拦截器的更多相关文章
- 从零开始学 Java - Spring AOP 拦截器的基本实现
一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...
- Spring使用拦截器支持国际化(转)
Spring使用拦截器支持国际化很方便,使用时只需要两个步骤: 一.spring配置 具体配置方式如下: <!-- 资源文件绑定器,文件名称:messages.properties(没有找到时的 ...
- Spring 中拦截器与过滤器的区别
spring 中拦截器 与servlet 的filter 有相似之处.比如二者都是aop 编程思想的体现都能实现权限检查,日志记录等. 不同之处 使用范围不同 Filter 是Servlet 规定的. ...
- Java - Spring AOP 拦截器的基本实现
一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...
- spring mvc拦截器原理分析
我的springMVC+mybatis中的interceptor使用@autowired注入DAO失败,导致报空指针错误,这个是为什么呢? :空指针说明没有注入进来,你可以检查一下你的这个拦截器int ...
- SSM(spring mvc+spring+mybatis)学习路径——2-2、spring MVC拦截器
目录 2-2 Spring MVC拦截器 第一章 概述 第二章 Spring mvc拦截器的实现 2-1 拦截器的工作原理 2-2 拦截器的实现 2-3 拦截器的方法介绍 2-4 多个拦截器应用 2- ...
- Spring 注解拦截器使用详解
Spring mvc拦截器 平时用到的拦截器通常都是xml的配置方式.今天就特地研究了一下注解方式的拦截器. 配置Spring环境这里就不做详细介绍.本文主要介绍在Spring下,基于注解方式的拦截器 ...
- spring mvc 拦截器的使用
Spring MVC 拦截器的使用 拦截器简介 Spring MVC 中的拦截器(Interceptor)类似于 Servler 中的过滤器(Filter).用于对处理器进行预处理和后处理.常用于日志 ...
- Spring boot拦截器的实现
Spring boot拦截器的实现 Spring boot自带HandlerInterceptor,可通过继承它来实现拦截功能,其的功能跟过滤器类似,但是提供更精细的的控制能力. 1.注册拦截器 @C ...
- 【Java Web开发学习】Spring MVC 拦截器HandlerInterceptor
[Java Web开发学习]Spring MVC 拦截器HandlerInterceptor 转载:https://www.cnblogs.com/yangchongxing/p/9324119.ht ...
随机推荐
- {Python之线程} 一 背景知识 二 线程与进程的关系 三 线程的特点 四 线程的实际应用场景 五 内存中的线程 六 用户级线程和内核级线程(了解) 七 python与线程 八 Threading模块 九 锁 十 信号量 十一 事件Event 十二 条件Condition(了解) 十三 定时器
Python之线程 线程 本节目录 一 背景知识 二 线程与进程的关系 三 线程的特点 四 线程的实际应用场景 五 内存中的线程 六 用户级线程和内核级线程(了解) 七 python与线程 八 Thr ...
- [No0000161]IDEA初步接触
安装 参考https://blog.csdn.net/qq_35246620/article/details/61191375 安装过程全程默认(路径和快捷方式自定义,不需要下载jre): 启动后全程 ...
- [No0000CF]想有一辈子花不完的钱?从了解“被动收入”开始吧
我想从理清自己所说被动收入的含义,开始创作此被动收入系列文章. 我更喜欢把被动收入较宽泛地定义为,甚至当你没有主动工作时,仍可赚取的收益.被动收入的另一个名称是剩余收入. 相比之下,当你停止工作时,通 ...
- 为单实例数据库配置ASM
环境配置沿用搭建RAC的环境配置 配置ASM可以在数据库软件安装之前进行,也可以在安装完数据库软件配置数据库前进行 [root@rac01 Packages]# cd /etc/yum.repos.d ...
- RequireJs的理解
什么是RequireJs RequireJS 是一个JavaScript模块加载器. 在ES6出现之前,JS不像其他语言同样拥有“模块”这一概念,于是为了支持JS模块化,出现了各种各样的语言工具,如w ...
- 洛谷P3250 网络 [HNOI2016] 整体二分
正解:整体二分+树状数组 解题报告: 传送门! 亲这里的建议是用整体二分呢 dbq最近看sd淘宝说话体看多了有点脑抽,,, 首先考虑如果是单组询问怎么做昂QAQ 考虑二分答案 对于所有比mid小的操作 ...
- Automation服务器不能创建对象(金税盘)
1. 安装防伪开票组件接口软件: 2. 把接口的注册文件放到%防伪开票系统的安装目录% \BIN下 3. 把%防伪开票系统的安装目录%\bin下的TaxCardX.dll文件复制至 c:\window ...
- js阻止默认事件,如a标签跳转和事件冒泡
禁止a标签点击跳转 <a href="http://baidu.com" onclick="return false">点我啊</a> ...
- ZedBoard上运行linux系统的准备工作框架
目标:ZedBoard上运行linux系统. 需要什么:图中上色部分. 应该做哪些工作:上色部分之前的所有步骤. 由上图得知,为了顺利在zedboard上构建嵌入式Linux操作系统,我们一般需要如下 ...
- php 7 新特性整理小结
php 7 比php 5 性能提升了很多,php 7 新特性主要表现在:1.变量存储字节减小,减少内存占用,提升变量操作速度:2.改善数组结构,数组元素和hash映射表被分配在同一块内存里,降低了内存 ...