Spring自定义一个拦截器类SomeInterceptor,实现HandlerInterceptor接口及其方法的实例

王朝学院·作者佚名  2016-08-27  
宽屏版  字体: 小 | 中 | 大 | 超大  

利用SPRing的拦截器可以在处理器Controller方法执行前和后增加逻辑代码,了解拦截器中preHandle、postHandle和afterCompletion方法执行时机。

自定义一个拦截器类SomeInterceptor,实现HandlerInterceptor接口及其方法。

然后在spring-mvc.xml中添加拦截器配置,来指定拦截哪些请求。

步骤一: 创建SomeInterceptor拦截器组件

新建一个com.souvc.interceptor包,在该包中新建一个SomeInterceptor类。SomeInterceptor类要实现HandlerInterceptor接口及其约定方法,代码如下:

packagecom.souvc.interceptor;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.web.servlet.HandlerInterceptor;importorg.springframework.web.servlet.ModelAndView;publicclassSomeInterceptorimplementsHandlerInterceptor {publicvoidafterCompletion(HttpServletRequest req,

HttpServletResponse res, Object handller, Exception e)throwsException {

System.out.println("请求处理完成后调用");

}publicvoidpostHandle(HttpServletRequest req, HttpServletResponse res,

Object handller, ModelAndView mv)throwsException {

System.out.println("处理器执行后调用");

}publicbooleanpreHandle(HttpServletRequest req, HttpServletResponse res,

Object handller)throwsException {

System.out.println("处理器执行前调用");returntrue;

}

}

步骤二:修改spring-mvc.xml配置

打开工程src下的spring-mvc.xml文件,追加SomeInterceptor配置。

<mvc:interceptors>

<mvc:interceptor>

<mvc:mapping path="/spring/*"/>

<mvc:exclude-mapping path="/login/*"/>

<beanclass="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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpahttp://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-3.2.xsdhttp://www.springframework.org/schema/mvchttp://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/*"/>

<beanclass="com.souvc.interceptor.SomeInterceptor"/>

</mvc:interceptor>

</mvc:interceptors>

</beans>

步骤三: 创建控制器HelloController中添加控制台打印信息

创建HelloController,在execute方法增加控制台打印语句。

packagecom.souvc.controller;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;

@Controller

@RequestMapping("/spring")publicclassHelloController {

@RequestMapping("/hello.form")publicString execute()throwsException {

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="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/CSS" 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

处理器执行后调用

请求处理完成后调用

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有