拦截器

实现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之拦截器的更多相关文章

  1. 从零开始学 Java - Spring AOP 拦截器的基本实现

    一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...

  2. Spring使用拦截器支持国际化(转)

    Spring使用拦截器支持国际化很方便,使用时只需要两个步骤: 一.spring配置 具体配置方式如下: <!-- 资源文件绑定器,文件名称:messages.properties(没有找到时的 ...

  3. Spring 中拦截器与过滤器的区别

    spring 中拦截器 与servlet 的filter 有相似之处.比如二者都是aop 编程思想的体现都能实现权限检查,日志记录等. 不同之处 使用范围不同 Filter 是Servlet 规定的. ...

  4. Java - Spring AOP 拦截器的基本实现

    一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...

  5. spring mvc拦截器原理分析

    我的springMVC+mybatis中的interceptor使用@autowired注入DAO失败,导致报空指针错误,这个是为什么呢? :空指针说明没有注入进来,你可以检查一下你的这个拦截器int ...

  6. SSM(spring mvc+spring+mybatis)学习路径——2-2、spring MVC拦截器

    目录 2-2 Spring MVC拦截器 第一章 概述 第二章 Spring mvc拦截器的实现 2-1 拦截器的工作原理 2-2 拦截器的实现 2-3 拦截器的方法介绍 2-4 多个拦截器应用 2- ...

  7. Spring 注解拦截器使用详解

    Spring mvc拦截器 平时用到的拦截器通常都是xml的配置方式.今天就特地研究了一下注解方式的拦截器. 配置Spring环境这里就不做详细介绍.本文主要介绍在Spring下,基于注解方式的拦截器 ...

  8. spring mvc 拦截器的使用

    Spring MVC 拦截器的使用 拦截器简介 Spring MVC 中的拦截器(Interceptor)类似于 Servler 中的过滤器(Filter).用于对处理器进行预处理和后处理.常用于日志 ...

  9. Spring boot拦截器的实现

    Spring boot拦截器的实现 Spring boot自带HandlerInterceptor,可通过继承它来实现拦截功能,其的功能跟过滤器类似,但是提供更精细的的控制能力. 1.注册拦截器 @C ...

  10. 【Java Web开发学习】Spring MVC 拦截器HandlerInterceptor

    [Java Web开发学习]Spring MVC 拦截器HandlerInterceptor 转载:https://www.cnblogs.com/yangchongxing/p/9324119.ht ...

随机推荐

  1. [No0000185]Java技术板块图

    .List 和 Set 的区别 .HashSet 是如何保证不重复的 .HashMap 是线程安全的吗,为什么不是线程安全的(最好画图说明多线程环境下不安全)? .HashMap 的扩容过程 .Has ...

  2. 关于Java程序流程控制的整理(已完善)

  3. Flume 在有赞大数据的实践

    https://mp.weixin.qq.com/s/gd0KMAt7z0WbrJL0RkMEtA 原创: 有赞技术 有赞coder 今天 文 | hujiahua on 大数据 一.前言 Flume ...

  4. 《mongoDB》基本操作-创建/更新/删除文档

    一:基本操作 - db; 当前选择的集合(等于数据库名) > db demo -  use db_name; 选择你要操作的集合 > use demo switched to db dem ...

  5. npm的源改成淘宝镜像

    修改源地址为淘宝 NPM 镜像npm config set registry http://registry.npm.taobao.org/ 修改源地址为官方源npm config set regis ...

  6. linux测试环境搭建步骤

    一.建用户 1.新建用户root用户登录,执行命令:useradd + 用户名 -m -d + 指定路径如:新建用户liuwq ,指定路径/home/ios命令:useradd liuwq -m -d ...

  7. rem设置

    html{ font-size:10vw; }

  8. (1.6)MySQL执行计划

    关键词:mysql执行计划 1.用法 [1.1]explain select * from tab_name........ [1.2]desc select * from tab_name..... ...

  9. Got timeout reading communication packets解决方法

    Got timeout reading communication packets解决方法 http://www.th7.cn/db/mysql/201702/225243.shtml [Note] ...

  10. 谷歌浏览器安装octotree插件

    Octotree Chrome安装与使用方法 Octotree Chrome作用: 主要使你在github查看项目时可以清晰明了的看到项目的结构以及具体代码,使下载代码更具有目的性,减少不必要代码的下 ...