spring之拦截器
拦截器
实现HandlerInterceptor接口:注册拦截器<mvc:inteceptors>
spring和springMVC父子容器的关系
在spring整体框架的核心概念中,容器是核心思想,就是用来管理Bean的整个生命周期的,而在一个项目中,容器不一定只有一个,spring中可以包括多个容器,而且容器有上下层关系,目前最常见的一种场景就是在项目中引入spring和springmvc两个框架,那么它其实就是两个容器,spring是父容器,springmvc是子容器,并且在spring父容器中注册的bean对于springmvc容器中是可见的,而在springmvc容器中注册的bean对于spring父容器中就是不可见的,也就是子容器可以看见父容器中注册的Bean,反之就不行。
全部拦截:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="${pageContext.request.contextPath }/springmvc/hello" method="post"> 11 姓名:<input type="text" name="username"><br> 12 年龄:<input type="text" name="age"><br> 13 <input type="submit" value="提交"> 14 </form> 15 </body> 16 </html>
1 package com.bjsxt.handlers; 2 3 import java.util.Map; 4 5 import org.springframework.context.annotation.Scope; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.ui.ModelMap; 9 import org.springframework.web.bind.annotation.RequestMapping; 10 11 //后台控制器 12 @Controller 13 @Scope("prototype") 14 @RequestMapping("/springmvc") 15 public class MyController{ 16 //接收json字符串封装成对象 17 @RequestMapping("/hello") 18 public String hello1(String username,int age,Model model,Map<String,Object>map,ModelMap modelMap){ 19 System.out.println(username+"______"+age); 20 model.addAttribute("username", username); 21 System.out.println("MyController.hello1()"); 22 map.put("age", age); 23 modelMap.addAttribute("gender", "female"); 24 System.out.println("MyController.hello1()"); 25 return "welcome"; 26 27 } 28 29 }
1 package com.bjsxt.interceptors; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.web.servlet.HandlerInterceptor; 7 import org.springframework.web.servlet.ModelAndView; 8 //自定义拦截器 9 public class FistInterceptor implements HandlerInterceptor { 10 //该方法执行时机:处理器方法执行之前 11 @Override 12 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, 13 Object handler) throws Exception { 14 System.out.println("拦截器执行"); 15 return false; 16 } 17 18 //该方法执行时机:所有方法处理完成执行之后,响应给浏览器客户端执行 19 @Override 20 public void afterCompletion(HttpServletRequest arg0, 21 HttpServletResponse arg1, Object arg2, Exception arg3) 22 throws Exception { 23 // TODO Auto-generated method stub 24 25 } 26 27 //该方法执行时机:处理器方法执行之后 28 @Override 29 public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, 30 Object arg2, ModelAndView arg3) throws Exception { 31 // TODO Auto-generated method stub 32 33 } 34 35 }
<?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>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 欢迎页面!${username}--${age}--${gender} 11 </body> 12 </html>
指定拦截
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="${pageContext.request.contextPath}/springmvc/hello2" method="POST"> 11 姓名:<input type="text" name="username"><br/> 12 年龄:<input type="text" name="age"><br/> 13 <input type="submit" value="提交"><br/> 14 </form> 15 </body> 16 </html>
1 package com.bjsxt.handlers; 2 3 import java.util.Map; 4 5 import org.springframework.context.annotation.Scope; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.ui.Model; 8 import org.springframework.ui.ModelMap; 9 import org.springframework.web.bind.annotation.RequestHeader; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 12 //后端控制器 13 @Controller 14 @Scope("prototype") 15 @RequestMapping("/springmvc") 16 public class MyController{ 17 @RequestMapping("/hello") 18 public String hello1(String username,int age,Model model,Map<String, Object> map,ModelMap modelMap){ 19 System.out.println(username + " ----------"+age); 20 model.addAttribute("username", username); 21 map.put("age", age); 22 modelMap.addAttribute("gender", "female"); 23 return "welcome"; 24 } 25 @RequestMapping("/hello2") 26 public String hello2(String username,int age,Model model,Map<String, Object> map,ModelMap modelMap){ 27 System.out.println(username + " 2222----------2222"+age); 28 model.addAttribute("username", username); 29 map.put("age", age); 30 modelMap.addAttribute("gender", "female"); 31 return "welcome"; 32 } 33 34 }
1 package com.bjsxt.interceptors; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.web.servlet.HandlerInterceptor; 7 import org.springframework.web.servlet.ModelAndView; 8 9 //自定义拦截器 10 public class FirstInterceptor implements HandlerInterceptor { 11 //该方法执行时机:处理器方法执行之前执行 12 @Override 13 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 14 throws Exception { 15 System.out.println("拦截器preHandle()执行!"); 16 return true; 17 } 18 19 //该方法执行时机:处理器方法执行之后执行 20 @Override 21 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, 22 ModelAndView modelAndView) throws Exception { 23 System.out.println("拦截器postHandle()执行!"); 24 25 } 26 27 //该方法执行时机:所有工作处理完成之后,响应给浏览器客户端之前执行 28 @Override 29 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) 30 throws Exception { 31 System.out.println("拦截器afterCompletion()执行!"); 32 33 } 34 35 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xmlns:aop="http://www.springframework.org/schema/aop" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans 10 http://www.springframework.org/schema/beans/spring-beans.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx.xsd 13 http://www.springframework.org/schema/aop 14 http://www.springframework.org/schema/aop/spring-aop.xsd 15 http://www.springframework.org/schema/mvc 16 http://www.springframework.org/schema/mvc/spring-mvc.xsd 17 http://www.springframework.org/schema/context 18 http://www.springframework.org/schema/context/spring-context.xsd"> 19 <!-- 注册组件扫描器 --> 20 <context:component-scan base-package="com.bjsxt.handlers"></context:component-scan> 21 <!-- 注册注解驱动 --> 22 <mvc:annotation-driven/> 23 <!-- 注册视图解析器 --> 24 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 25 <property name="prefix" value="/jsp/"></property> 26 <property name="suffix" value=".jsp"></property> 27 </bean> 28 29 <!-- 注册拦截器 --> 30 <mvc:interceptors> 31 <mvc:interceptor> 32 <mvc:mapping path="/springmvc/hello"/> 33 <mvc:mapping path="/**"/> 34 <mvc:exclude-mapping path="/springmvc/hello2"/> 35 <bean id="" class="com.bjsxt.interceptors.FirstInterceptor"></bean> 36 </mvc:interceptor> 37 </mvc:interceptors> 38 39 <!-- 静态资源无法访问第二种解决方案 --> 40 <!-- <mvc:default-servlet-handler/> --> 41 <!-- 静态资源无法访问第三种解决方案 --> 42 <mvc:resources location="/images/" mapping="/images/**"></mvc:resources> 43 <mvc:resources location="/js/" mapping="/js/**"></mvc:resources> 44 </beans>
个人学习笔记,记录日常学习,便于查阅及加深,仅为方便个人使用。