Java笔记之SpringMVC(六):拦截器
0.说在前面
1.新建包com.springmvc.demo.interceptor,并新建RequestInterceptor类,实现HandlerInterceptor接口
package com.springmvc.demo.interceptor; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; public class RequestInterceptor implements HandlerInterceptor{ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle在Controller之前被调用......"); return HandlerInterceptor.super.preHandle(request, response, handler); } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle在Controller被调用返回ModelAndView之后被调用......"); //在返回的ModelAndView对象中新加一个数据对象 modelAndView.addObject("currentDate", new Date()); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion在视图被访问之后被调用......"); } }
2.修改springmvc.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 开启注解扫描, base-package配置Controller所在的基础包--> <context:component-scan base-package="com.springmvc.demo.controller"></context:component-scan> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 配置文件上传解析器,设置最大文件大小为10M(10*1024*1024) --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"></property> </bean> <!-- 配置拦截器,其中可以配置多个拦截器 --> <mvc:interceptors> <!-- 在其中完成一个拦截器的配置 --> <mvc:interceptor> <!--拦截器拦截的请求,这里配置的是所有SpringMVC的请求 --> <mvc:mapping path="/*.action"/> <!-- 拦截器放行的请求,对该请求不进行拦截 --> <mvc:exclude-mapping path="/login.action"/> <!-- 配置使用的拦截器类 --> <bean class="com.springmvc.demo.interceptor.RequestInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> </beans>
3.新建LoginController类
package com.springmvc.demo.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class LoginController { @RequestMapping("/login.action") public String doLogin(HttpServletRequest request){ System.out.println("LoginController的doLogin方法被访问了......"); request.setAttribute("message", "登陆成功"); return "success"; } @RequestMapping("/login2.action") public String doLogin2(HttpServletRequest request){ System.out.println("LoginController的doLogin2方法被访问了......"); request.setAttribute("message", "登陆成功"); return "success"; } }
4.修改success.jsp页面,添加获取ModelAndView中currentDate的值和控制台打印的Java代码
<%@ 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>成功页面</title> </head> <body> ${message }---${currentDate } <% System.out.println("视图已经被访问了......"); %> </body> </html>
5.启动项目,首先访问http://localhost:8080/springmvc_demo/login.action
从页面信息和后台信息打印可以看出,访问/login.action并没有被拦截器拦截,拦截器中的一系列打印信息和设置的currentDate都没有执行.
6.访问http://localhost:8080/springmvc_demo/login2.action
从页面信息和后台信息可以看出/login2.action被拦截器拦截了,其中的操作也都执行了,设置的日期信息在页面也正常可以获取到.拦截器中的三个方法的执行顺序也在打印信息中得到印证.