Spring customizes an interceptor class SomeInterceptor, implements an instance of the HandlerInterceptor interface and its method, and handlerinterceptor
The Spring interceptor can be used to add logic code before and after the Controller Method of the processor to understand the execution time of the preHandle, postHandle, and afterCompletion methods in the interceptor.
Customizes an interceptor class SomeInterceptor to implement the HandlerInterceptor interface and its methods.
Then add the interceptor configuration in the spring-mvc.xml to specify which requests to intercept.
Step 1: Create the SomeInterceptor interceptor component
Create a com. souvc. interceptor package and create a SomeInterceptor class in the package. The SomeInterceptor class implements the HandlerInterceptor interface and its agreed methods. The Code is as follows:
Package com. souvc. interceptor; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. springframework. web. servlet. handlerInterceptor; import org. springframework. web. servlet. modelAndView; public class SomeInterceptor implements HandlerInterceptor {public void afterCompletion (HttpServletRequest req, HttpServletResponse res, Object Handler, Exception e) throws Exception {System. out. println ("called after request processing");} public void postHandle (HttpServletRequest req, HttpServletResponse res, Object Handler, ModelAndView mv) throws Exception {System. out. println ("called after processor execution");} public boolean preHandle (HttpServletRequest req, HttpServletResponse res, Object Handler) throws Exception {System. out. println ("called before processor execution"); return true ;}}
Step 2: Modify spring-mvc.xml configurations
Open the spring-mvc.xml file under the src project and append the SomeInterceptor configuration.
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/spring/*"/> <mvc:exclude-mapping path="/login/*"/> <bean class="com.souvc.interceptor.SomeInterceptor"/> </mvc:interceptor> </mvc:interceptors>
Spring-mvc.xml source code:
<? 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: jpa = "http://www.springframework.org/schema/data/jpa" xmlns: Util = "http://www.springframework.org/schema/util" xmlns: mvc = "http://www.springframework.org/schema/mvc" 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 Rg/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd "> <util: properties id =" jdbcProps "location =" classpath: db. properties "/> <context: component-scan base-package =" com. souvc "/> <! -- View processing --> <bean id = "viewResolver" class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" prefix "value ="/WEB-INF/jsp/"> </property> <property name =" suffix "value = ". jsp "> </property> </bean> <mvc: interceptors> <mvc: interceptor> <mvc: mapping path ="/spring/* "/> <mvc: exclude-mapping path = "/login/*"/> <bean class = "com. souvc. interceptor. someInterceptor "/> </mvc: interceptor> </mvc: interceptors> </beans>
Step 3: Create a controller and add console printing information to HelloController.
Create HelloController and add the console print statement in the execute method.
Package com. souvc. controller; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; @ Controller @ RequestMapping ("/spring") public class HelloController {@ RequestMapping ("/hello. form ") public String execute () throws Exception {System. out. println ("execute HelloController"); return "hello ";}}
Add the hello. jsp page under the jsp directory under the WebRoot directory:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
The web. xml file is as follows:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.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:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.form</url-pattern> </servlet-mapping></web-app>
Step 4: deploy and access the project
Start Tomcat after the project is deployed. Enter the address http: // localhost: 8080/SpringValues/spring/hello. form in the browser and observe the output result on the console.
Before the processor executes the call, the HelloController processor executes the call, and then the call request is processed.