Spring MVC 拦截器 HandlerInterceptorAdapter、HandlerInterceptor 示例
Spring Interceptor 用于拦截客户端请求并处理它们。有时我们希望拦截 HTTP 请求并在将其交给控制器处理程序方法之前进行一些处理。这就是 Spring MVC Interceptor 派上用场的地方。
春季拦截器
就像我们有Struts2 拦截器一样,我们可以通过实现org.springframework.web.servlet.HandlerInterceptor
接口或重写org.springframework.web.servlet.handler.HandlerInterceptorAdapter
提供 HandlerInterceptor 接口基实现的抽象类来创建我们自己的 Spring 拦截器。
Spring 拦截器 - HandlerInterceptor
Spring HandlerInterceptor根据我们想要拦截 HTTP 请求的位置声明了三种方法。
- boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler):此方法用于在将请求交给处理程序方法之前对其进行拦截。此方法应返回“true”,以让 Spring 知道通过另一个 spring 拦截器处理请求,或者如果没有其他 spring 拦截器,则将其发送到处理程序方法。如果此方法返回“false”,Spring 框架将假定请求已由 spring 拦截器本身处理,不需要进一步处理。在这种情况下,我们应该使用响应对象将响应发送到客户端请求。对象处理程序是处理请求的选定处理程序对象。此方法也可以抛出异常,在这种情况下,Spring MVC 异常处理应该有助于将错误页面作为响应发送。
- void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView):当 HandlerAdapter 调用处理程序但 DispatcherServlet 尚未呈现视图时,将调用此 HandlerInterceptor 拦截器方法。此方法可用于向 ModelAndView 对象添加附加属性,以便在视图页面中使用。我们可以使用此 spring 拦截器方法来确定处理程序方法处理客户端请求所需的时间。
- void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex):这是一个 HandlerInterceptor 回调方法,在执行处理程序并呈现视图后调用。
如果配置了多个 Spring 拦截器,则preHandle()方法按配置顺序执行,而postHandle()和afterCompletion()方法则按相反顺序调用。让我们创建一个简单的 Spring MVC 应用程序,我们将在其中配置一个 Spring 拦截器来记录控制器处理程序方法的时间。我们最终的 Spring 拦截器示例项目将如下图所示,我们将研究我们感兴趣的组件。
Spring 拦截器 - 控制器类
package com.journaldev.spring;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
//adding some time lag to check interceptor execution
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
logger.info("Before returning view page");
return "home";
}
}
我只是在处理程序方法的执行中添加了一些处理时间来检查我们的 spring 拦截器方法的运行情况。
Spring MVC 拦截器 - HandlerInterceptorAdapter 实现
为了简单起见,我扩展了抽象类HandlerInterceptorAdapter
。HandlerInterceptorAdapter 是 HandlerInterceptor 接口的抽象适配器类,用于简化仅限前/仅限后拦截器的实现。
package com.journaldev.spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class RequestProcessingTimeInterceptor extends HandlerInterceptorAdapter {
private static final Logger logger = LoggerFactory
.getLogger(RequestProcessingTimeInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
long startTime = System.currentTimeMillis();
logger.info("Request URL::" + request.getRequestURL().toString()
+ ":: Start Time=" + System.currentTimeMillis());
request.setAttribute("startTime", startTime);
//if returned false, we need to make sure 'response' is sent
return true;
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("Request URL::" + request.getRequestURL().toString()
+ " Sent to Handler :: Current Time=" + System.currentTimeMillis());
//we can add attributes in the modelAndView and use that in the view page
}
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
long startTime = (Long) request.getAttribute("startTime");
logger.info("Request URL::" + request.getRequestURL().toString()
+ ":: End Time=" + System.currentTimeMillis());
logger.info("Request URL::" + request.getRequestURL().toString()
+ ":: Time Taken=" + (System.currentTimeMillis() - startTime));
}
}
逻辑非常简单,我只是记录处理程序方法执行的时间和处理请求(包括呈现视图页面)所花费的总时间。
Spring MVC 拦截器配置
我们必须将 spring 拦截器连接到请求,我们可以使用mvc:interceptors元素来连接所有拦截器。我们还可以在通过映射元素为请求包含 spring 拦截器之前提供要匹配的 URI 模式。我们最终的 spring bean 配置文件 (spring.xml) 如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="https://www.springframework.org/schema/mvc"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:beans="https://www.springframework.org/schema/beans"
xmlns:context="https://www.springframework.org/schema/context"
xsi:schemaLocation="https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Configuring interceptors based on URI -->
<interceptors>
<interceptor>
<mapping path="/home" />
<beans:bean class="com.journaldev.spring.RequestProcessingTimeInterceptor"></beans:bean>
</interceptor>
</interceptors>
<context:component-scan base-package="com.journaldev.spring" />
</beans:beans>
我不会解释 Web 应用程序的所有其他组件,因为我们对它们不感兴趣,并且它们没有任何特定的 Spring 拦截器相关配置。
Spring MVC 拦截器应用程序测试
只需在 servlet 容器中部署应用程序并调用主控制器,您将看到如下所示的记录器输出。
INFO : com.journaldev.spring.RequestProcessingTimeInterceptor - Request URL::https://localhost:9090/SpringInterceptors/home:: Start Time=1396906442086
INFO : com.journaldev.spring.HomeController - Welcome home! The client locale is en_US.
INFO : com.journaldev.spring.HomeController - Before returning view page
Request URL::https://localhost:9090/SpringInterceptors/home Sent to Handler :: Current Time=1396906443098
INFO : com.journaldev.spring.RequestProcessingTimeInterceptor - Request URL::https://localhost:9090/SpringInterceptors/home:: End Time=1396906443171
INFO : com.journaldev.spring.RequestProcessingTimeInterceptor - Request URL::https://localhost:9090/SpringInterceptors/home:: Time Taken=1085
输出确认 Spring 拦截器方法按定义的顺序执行。这就是使用 Spring 拦截器的全部内容,您可以从以下链接下载 Spring Interceptor 示例项目,并尝试使用多个拦截器并按不同的配置顺序进行检查。
下载 Spring Interceptors 项目