利用SPRing的攔截器可以在處理器Controller方法執行前和后增加邏輯代碼,了解攔截器中preHandle、postHandle和afterCompletion方法執行時機。
自定義一個攔截器類SomeInterceptor,實現HandlerInterceptor接口及其方法。
然后在spring-mvc.xml中添加攔截器配置,來指定攔截哪些請求。
步驟一: 創建SomeInterceptor攔截器組件
新建一個com.souvc.interceptor包,在該包中新建一個SomeInterceptor類。SomeInterceptor類要實現HandlerInterceptor接口及其約定方法,代碼如下:
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 handller, Exception e) throws Exception { System.out.println("請求處理完成后調用"); } public void postHandle(HttpServletRequest req, HttpServletResponse res, Object handller, ModelAndView mv) throws Exception { System.out.println("處理器執行后調用"); } public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handller) throws Exception { System.out.println("處理器執行前調用"); return true; }}
步驟二:修改spring-mvc.xml配置
打開工程src下的spring-mvc.xml文件,追加SomeInterceptor配置。
<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 源碼:
<?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 http://www.springframework.org/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" /> <!-- 視圖處理 --> <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>
步驟三: 創建控制器HelloController中添加控制臺打印信息
創建HelloController,在execute方法增加控制臺打印語句。
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("執行HelloController"); return "hello"; }}
在WebRoot目錄下的jsp目錄下添加hello.jsp頁面:
<%@ 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"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'hello.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keyWords" content="keyword1,keyword2,keyword3"> <meta http-equiv="descrCSS" href="styles.css"> --> </head> <body> hello<br> </body></html>
web.xml文件如下:
<?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>
步驟四:部署并訪問工程
部署工程后啟動Tomcat,在瀏覽器中輸入地址http://localhost:8080/SpringValues/spring/hello.form,觀察控制臺輸出結果。
處理器執行前調用執行HelloController處理器執行后調用請求處理完成后調用
新聞熱點
疑難解答