后端开发基础-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工程,