spring之攔截器
阿新 • • 發佈:2018-12-30
攔截器
實現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>