Spring MVC’s handler interceptor is like a good friend and will help in time of need. Spring’s handler interceptor as rightly named, intercepts a request, html
Spring’s interceptor can be configured for all the requests (for any URI’s requested) or for a group of URI’s (may be for a set of modules, etc.). Just remember controller and handler are the same. If you are a beginner in Spring, to better understand interceptor, please go through the Spring 3 MVC tutorial. java
In real scenario, Spring MVC handler interceptors are used for authentication, logging, to add a common message to all response. For the pages displayed we want to remove all bold tags from the response, it is possible using Spring interceptor. web
Note: My opinion on the interceptor spring configuration is, it still follows the old XML based configuration type. It will be better if the annotation based configuration is also brought into this interceptor declaration as we are doing for the controllers. spring
In this below example, express
packagecom.javapapers.spring.mvc.interceptor;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.springframework.stereotype.Component;
importorg.springframework.web.servlet.handler.HandlerInterceptorAdapter;
@Component
publicclassAnimalInterceptorextendsHandlerInterceptorAdapter {
publicbooleanpreHandle(HttpServletRequest request,
HttpServletResponse response, Object handler)throwsException {
System.out.println("AnimalInterceptor: REQUEST Intercepted for URI: "
+ request.getRequestURI());
request.setAttribute("special","I Love Animals!");
returntrue;
}
}
|
packagecom.javapapers.spring.mvc.interceptor;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.springframework.stereotype.Component;
importorg.springframework.web.servlet.handler.HandlerInterceptorAdapter;
@Component
publicclassGreetingInterceptorextendsHandlerInterceptorAdapter {
publicbooleanpreHandle(HttpServletRequest request,
HttpServletResponse response, Object handler)throwsException {
System.out.println("GreetingInterceptor: REQUEST Intercepted for URI: "
+ request.getRequestURI());
request.setAttribute("greeting","Happy Diwali!");
returntrue;
}
}
|
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<context:component-scanbase-package="com.javapapers.spring.mvc"/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="prefix"value="/view/"/>
<propertyname="suffix"value=".jsp"/>
<propertyname="order"value="1"/>
</bean>
<mvc:interceptors>
<beanclass="com.javapapers.spring.mvc.interceptor.GreetingInterceptor"/>
<mvc:interceptor>
<mvc:mappingpath="/AnimalList"/>
<beanclass="com.javapapers.spring.mvc.interceptor.AnimalInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
|
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"version="2.5">
<display-name>Spring MVC Excel Export</display-name>
<servlet>
<servlet-name>springMVCDispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVCDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
|
packagecom.javapapers.spring.mvc.controller;
importorg.springframework.stereotype.Controller;
importorg.springframework.ui.Model;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.RequestParam;
@Controller
publicclassHelloWorldController {
@RequestMapping("/")
publicString hello() {
return"hello";
}
@RequestMapping(value ="/hi", method = RequestMethod.GET)
publicString hi(@RequestParam("name") String name, Model model) {
String message ="Hi "+ name +"!";
model.addAttribute("message", message);
return"hi";
}
}
|
packagecom.javapapers.spring.mvc.controller;
importjava.util.List;
importorg.springframework.stereotype.Controller;
importorg.springframework.ui.Model;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importcom.javapapers.spring.mvc.Animal;
importcom.javapapers.spring.mvc.AnimalService;
@Controller
publicclassZooController {
protectedAnimalService animalService =newAnimalService();
@RequestMapping(value ="/AnimalList", method = RequestMethod.GET)
publicString getAnimals(Model model) {
List<Animal> animalList = animalService.getAnimalList();
model.addAttribute("animalList", animalList);
return"AnimalList";
}
}
|
AnimalList.jsp:
<html>
<body>
<em>Welcome! <c:outvalue="${greeting}"></c:out> <c:outvalue="${special}"></c:out></em>
<hr/>
<h1>Example for Spring MVC Excel Export</h1>
<h2>Animal List</h2>
...
hi.jsp:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html><head><title>Result</title></head><body>
<em>Welcome! <c:outvalue="${greeting}"></c:out> <c:outvalue="${special}"></c:out></em>
<hr/>
<h1><c:outvalue="${message}"></c:out></h1>
<h2><ahref="./AnimalList">Animal List</a></h2>
</body></html>
hello.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html><head><title>Home</title></head><body>
<em>Welcome! <c:outvalue="${greeting}"></c:out> <c:outvalue="${special}"></c:out></em>
<hr/><h1>Hello World!</h1><hr/>
<h2><ahref="./AnimalList">Animal List</a></h2><hr/>
<formaction="hi">Name: <inputtype="text"name="name">
<inputtype="submit"value="Submit"></form>
</body></html>
|
Animal.java and AnimalService.java are of not much interest to the Spring MVC interceptor topic, but required to run the example and included in the download below. api
The log statements we have added in the Spring interceptor classes will come in log as follows, spring-mvc
GreetingInterceptor: REQUEST Intercepted for URI: /springmvcinterceptor/
GreetingInterceptor: REQUEST Intercepted for URI: /springmvcinterceptor/AnimalList
AnimalInterceptor: REQUEST Intercepted for URI: /springmvcinterceptor/AnimalList session