1. 程式人生 > >後端開發基礎-SpringMVC框架學習-009——基礎概念

後端開發基礎-SpringMVC框架學習-009——基礎概念

攔截器

什麼是攔截器?

前端控制器會先呼叫攔截器,然後呼叫處理器。 注: 過濾器是servlet規範中定義的一種特殊的元件, 會攔截servlet容器的呼叫過程。

如何寫一個攔截器?

step1. 寫一個java類,實現HandlerInterceptor 介面。 step2. 將攔截處理邏輯寫在以下幾個方法 裡面: . preHandle方法:前端控制器先呼叫preHandler 方法,然後再呼叫處理器的方法。如果該方法 返回值為true,表示繼續向後呼叫,否則,處理 結束。 . postHandle方法:處理器的方法已經執行完畢, 正準備返回ModelAndView物件給前端控制器時 執行。所以,可以在該方法裡面,修改ModelAndView。 (比如,修改處理結果或者檢視名) . afterCompletion方法:整個處理流程當中最後 執行的方法。 step3. 配置攔截器

案例演示:

工程案例目錄結構

匯入Tomcat類庫 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.study</groupId>
  <artifactId>springcase-day06</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
   <dependencies>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-webmvc</artifactId>
  		<version>3.2.8.RELEASE</version>
  	</dependency>
  </dependencies>
</project>

 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>springcase-day06</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:app.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

app.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:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
		
		<!-- 配置元件掃描 -->
		<context:component-scan base-package="controller"/>
		<!-- 配置mvc註解掃描 -->
		<mvc:annotation-driven/>
		
		<!-- 
			配置檢視解析器。
			負責將檢視名解析成真正的檢視物件(比如jsp)。
		 -->
		 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		 	<property name="prefix" value="/WEB-INF/"/>
		 	<property name="suffix" value=".jsp"/>
		 </bean>
		 
		 <!-- 配置攔截器
		 	a.可以在interceptors元素裡面
		 	配置多個攔截器。
		 	b.這些攔截器會按照配置的先後順序來
		 	執行。
		 	c.攔截器多層請求:比如/demo/hello2.do
		 	應該使用"/**".
		  -->
		  <mvc:interceptors>
		  	<mvc:interceptor>
		  		<mvc:mapping path="/**"/>
		  		<bean class="interceptors.SomeInterceptor" />
		  	</mvc:interceptor>
		  	
		  </mvc:interceptors>
		  
		 
</beans>

HelloController.java

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {
	
	@RequestMapping("/hello1.do")
	public String hello1(){
		System.out.println("HelloController的hello1方法...");
		return "hello";
	}
	
	@RequestMapping("/demo/hello2.do")
	public String hello2(){
		System.out.println("HelloController的hello2方法...");
		return "hello";
	}

}

SomeInterceptor.java

package interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
 * 攔截器
 * @author Cher_du
 *
 */
public class SomeInterceptor implements HandlerInterceptor{

	/**
	 * 前端控制器先呼叫preHandle
	 * 方法,然後再呼叫處理器的方法。如果該方法
	 * 返回值為true,表示繼續向後呼叫,否則,處理
	 * 結束。
	 * arg2:處理器方法物件(Method)
	 */
	public boolean preHandle(HttpServletRequest request, 
			HttpServletResponse response,
			Object arg2)
			throws Exception {
		System.out.println("SomeInterceptor的preHandle方法...");System.out.println(arg2.getClass());
		return true;
	}

	
	/**
	 * 處理器的方法已經執行完畢,
	 * 正準備返回ModelAndView物件給前端控制器時
	 * 執行。所以,可以在該方法裡面,修改ModelAndView。
	 * (比如,修改處理結果或者檢視名)。
	 * arg2:處理器方法物件(Method)。
	 */
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response,
			Object arg2,
			ModelAndView modelAndView) throws Exception {
		System.out.println("SomeInterceptor的postHandle方法...");
		
	}

	/**
	 * 整個處理流程當中最後執行的方法。
	 * arg2:處理器方法物件(Method)。
	 * ex:處理器的方法產生的異常。
	 */
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, 
			Object arg2, 
			Exception ex)
			throws Exception {
		System.out.println("SomeInterceptor的afterCompletion方法...");
		
	}

}

 啟動Tomcat 執行 springcase-day06工程