利用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="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
处理器执行后调用
请求处理完成后调用

Spring自定义一个拦截器类SomeInterceptor,实现HandlerInterceptor接口及其方法的实例的更多相关文章

  1. SpringMVC 自定义一个拦截器

    自定义一个拦截器方法,实现HandlerInterceptor方法 public class FirstInterceptor implements HandlerInterceptor{ /** * ...

  2. spring自定义注解拦截器的配置

    1.创建注解文件 (文件格式为注解) 这里面什么都不需要写 文件名就是注解名称 如下 是@anno package com.ABC123.anno; import java.lang.annotati ...

  3. spring boot 添加拦截器的简单实例(springBoot 2.x版本,添加拦截器,静态资源不可访问解决方法)

    spring中拦截器主要分两种,一个是HandlerInterceptor,一个是MethodInterceptor 一.HandlerInterceptor HandlerInterceptor是s ...

  4. spring boot的拦截器简单使用

    1.spring boot拦截器默认有: HandlerInterceptorAdapter AbstractHandlerMapping UserRoleAuthorizationIntercept ...

  5. Spring Boot2.0拦截器简单实现判断是否登录

    在进行项目开发的时候使用springboot框架用到拦截器时发现2.0以后原来的抽象类WebMvcConfigurerAdapter已经过时了,去官网查文档2.x版本要实现拦截器功能改为需要继承Web ...

  6. springmvc自定义的拦截器以及拦截器的配置

    一.自定义拦截器 Spring MVC也可以使用拦截器对请求进行拦截处理,用户可以自定义拦截器来实现特定的功能,自定义的拦截器必须实现HandlerInterceptor接口. 二.HandlerIn ...

  7. Mybatis自定义SQL拦截器

    本博客介绍的是继承Mybatis提供的Interface接口,自定义拦截器,然后将项目中的sql拦截一下,打印到控制台. 先自定义一个拦截器 package com.muses.taoshop.com ...

  8. (转)spring中的拦截器(HandlerInterceptor+MethodInterceptor)

    1.  过滤器跟拦截器的区别 在说拦截器之前,不得不说一下过滤器,有时候往往被这两个词搞的头大. 其实我们最先接触的就是过滤器,还记得web.xml中配置的<filter>吗~ 你应该知道 ...

  9. spring boot 添加拦截器

    构建一个spring boot项目. 添加拦截器需要添加一个configuration @Configuration @ComponentScan(basePackageClasses = Appli ...

随机推荐

  1. 使用jQueryUI的dialog实现一个提示功能

    信息提示给用户是程序开发中,最常用的一个功能. Insus.NET使用jQueryUI的dialog来实现一个,可以定义标题,对话框的大小等. 在ASP.NET MVC环境下来演示吧. 在Octobe ...

  2. 转帖一篇sixxpack破解的文章!

    星期天闲着没事玩游戏,玩游戏不能无外挂.于是百度了半天,找到了一个,看介绍貌似不错,就下载了下来.一看,竟然是用.net写的,下意识地Reflector了一下.发现竟是一个叫actmp的程序集.如图: ...

  3. sql 随机抽取几条数据的方法 推荐

    传说用这个语句管用:select top 5 * from tablename order by newid() 我放到sql的查询分析器里去执行果然管用,随机抽取5条信息,不停的换,结果我应用到程序 ...

  4. Dev gridView中设置自适应列宽和日期显示格式、金额的显示格式

    在Dev GridView控件中,数据库中表数据日期都是长日期格式(yyyy-MM-dd HH:mm:ss),但显示在控件变成短日期格式(yyyy-MM-dd),金额显示要显示精确的数值, 比如80. ...

  5. 使用PreparedStatement执行SQL语句时占位符(?)的用法

    1.Student数据库表 ID  name gender       2.Java代码 public static void main(String[] args) { int _id=1; Str ...

  6. 阿里云主机上安装jdk

    今天继续安装jdk到阿里云服务上,大家要看一下阿里云是32位还是64位的,如果是32位下载32位的包,如果是64位的下载64位的包 我的就是64位的,开始我还不知道是怎么区分32/64位的,原来X64 ...

  7. Android笔记——Android中数据的存储方式(二)

    我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...

  8. [TypeScript] JSON对象转TypeScript对象范例

    [TypeScript] JSON对象转TypeScript对象范例 Playground http://tinyurl.com/nv4x9ak Samples class DataTable { p ...

  9. 移动端调试工具-Debuggap

    随着移动互联网的迅速崛起,开发移动应用程序越来越多,但如果在移动端开发应用程序需要调试时,额- 仿佛又回到了IE时代,最方便也只能到处 alert 来调试.目前已经有一款产品可以做到这一点,比如pho ...

  10. ABAP中使用浏览器打开网页

    在SAP ABAP中可以在Screen中嵌入Html control打开网页,也可以通过调用本地的IE浏览器打开. 1.在Screen中嵌入Html control的例子,在系统中有,se38:SAP ...