springmvc自定义两个拦截器的案例

package com.itheima.web.icontroller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller("InterceptorController")
@RequestMapping("/springmvc")
public class InterceptorController {
	
	@RequestMapping("/testInterceptor")
	public String testInterceptor() {
		System.out.println("testInterceptor方法执行了...");

		return "success";
	}
}

InterceptorController控制器类

 

package com.itheima.web.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
 * 自定义拦截器
 * @author ChonZ
 *
 */

public class MyInterceptor1 implements HandlerInterceptor {
	/**
	 * 末端执行
	 * 
	 * 如何调用:
	 * 		按拦截器定义(springmvc.xml配置)逆序调用
	 * 何时调用:
	 * 		只有preHandler返回true才调用
	 * 有什么用:
	 * 		在DispatcherServlet完全处理完请求后被调用
	 * 		可以在该方法中进行一些资源清理的操作
	 * 当preHandle方法中返回值为true,afterCompletion都会执行
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println("拦截器111111中的afterCompletion方法执行了..");
	}
	
	
	/**
	 * 中端执行
	 * 
	 * 如何调用:
	 * 		按拦截器定义(springmvc.xml配置)逆序调用
	 * 何时调用:
	 * 		在拦截器链内所有拦截器返回成功调用
	 * 有什么用:
	 * 		在业务处理器完成请求后,但是DispatcherServlet向客户端返回响应前被调用,
	 * 		在该方法中对用户请求request进行处理
	 * 
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println("拦截器111111中的postHandle方法执行了..");
	}
	
	/**
	 * 头部执行
	 * 
	 * 如何调用:
	 * 		拦截器定义(springmvc.xml配置)顺序调用
	 * 何时调用:
	 * 		只要配置了都会调用
	 * 有什么用:
	 * 		如果程序员决定该拦截器对请求进行拦截处理后还要调用其他的拦截器,或者业务处理器去进行处理,则返回true
	 *		如果程序员决定不需要再调用其他的组件去处理请求,则返回false
	 *
	 * 当请求到达时,先执行此方法,在执行handler方法
	 * 此方法的返回值决定了是否放行,可以用在用户登录前后的操作
	 * 返回true:放行 放行的含义:如果有下一个拦截器执行下一个,如果该拦截器处于最后一个,则执行handler方法(控制器的方法)
	 * 返回false:不放行
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		System.out.println("拦截器111111中的preHandler方法执行了..");
		return false;
	}

}

MyInterceptor1自定义拦截器类

 

package com.itheima.web.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
 * 自定义拦截器
 * @author ChonZ
 *
 */

public class MyInterceptor2 implements HandlerInterceptor {
	/**
	 * 末端执行
	 * 
	 * 如何调用:
	 * 		按拦截器定义(springmvc.xml配置)逆序调用
	 * 何时调用:
	 * 		只有preHandler返回true才调用
	 * 有什么用:
	 * 		在DispatcherServlet完全处理完请求后被调用
	 * 		可以在该方法中进行一些资源清理的操作
	 * 当preHandle方法中返回值为true,afterCompletion都会执行
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println("拦截器22222中的afterCompletion方法执行了..");
	}
	
	
	/**
	 * 中端执行
	 * 
	 * 如何调用:
	 * 		按拦截器定义(springmvc.xml配置)逆序调用
	 * 何时调用:
	 * 		在拦截器链内所有拦截器返回成功调用
	 * 有什么用:
	 * 		在业务处理器完成请求后,但是DispatcherServlet向客户端返回响应前被调用,
	 * 		在该方法中对用户请求request进行处理
	 * 
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println("拦截器22222中的postHandle方法执行了..");
	}
	
	/**
	 * 头部执行
	 * 
	 * 如何调用:
	 * 		拦截器定义(springmvc.xml配置)顺序调用
	 * 何时调用:
	 * 		只要配置了都会调用
	 * 有什么用:
	 * 		如果程序员决定该拦截器对请求进行拦截处理后还要调用其他的拦截器,或者业务处理器去进行处理,则返回true
	 *		如果程序员决定不需要再调用其他的组件去处理请求,则返回false
	 *
	 * 当请求到达时,先执行此方法,在执行handler方法
	 * 此方法的返回值决定了是否放行,可以用在用户登录前后的操作
	 * 返回true:放行 放行的含义:如果有下一个拦截器执行下一个,如果该拦截器处于最后一个,则执行handler方法(控制器的方法)
	 * 返回false:不放行
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		System.out.println("拦截器22222中的preHandler方法执行了..");
		return false;
	}

}

MyInterceptor2 自定义拦截器类

 

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd">
		<!-- 告知spring创建容器时要扫描的包 -->
		<context:component-scan base-package="com.itheima"></context:component-scan>
		
		<!-- 配置springmvc的视图解析器,用于找到响应的jsp -->
		<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/WEB-INF/pages/"></property>
			<property name="suffix" value=".jsp"></property>
		</bean>
		
		<!-- 配置springmvc的拦截器 -->
		<mvc:interceptors>
			<mvc:interceptor>
				<!-- 用于指定拦截那些请求映射 -->
				<mvc:mapping path="/**"/>
				<!-- 配置拦截器 -->
				<bean id="myInterceptor1" class="com.itheima.web.interceptor.MyInterceptor1"/>
			</mvc:interceptor>
			
			<mvc:interceptor>
				<!-- 用于指定拦截那些请求映射 -->
				<mvc:mapping path="/**"/>
				<!-- 配置拦截器 -->
				<bean id="myInterceptor2" class="com.itheima.web.interceptor.MyInterceptor2"/>
			</mvc:interceptor>
		</mvc:interceptors>
		
		<!-- 开启springmvc的注解  -->
		<mvc:annotation-driven></mvc:annotation-driven>
		
		<!-- 设置静态资源不参与springmvc编码的过滤 -->
		<mvc:resources location="/css/" mapping="/css/**"/>
		<mvc:resources location="/images/" mapping="/images/**"/>
		<mvc:resources location="/scripts/" mapping="/javascripts/**"/>
</beans>

springmvc.xml配置文件配置拦截器

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成功页面</title>
</head>
<body>
执行成功
</body>
</html>

success.jsp测试页面

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	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>day02_springmvc5_01response</display-name>


	<!-- 配置springmvc核心控制器 -->
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

		<!-- 加载springmvc配置文件 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<!-- 请求过滤器,把请求交给springmvc处理 -->
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<!-- 配置springmvc提供的编码过滤器 -->
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

web.xml配置文件

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>springmvc的拦截器</title>
</head>
<body>

<a href="springmvc/testInterceptor">测试springmvc的自定义拦截器</a>
	
</body>
</html>

index.jsp测试页面

 

输出结果:

1.MyInterceptor1和MyInterceptor2 的preHandle返回值为false

拦截器111111中的preHandler方法执行了..(MyInterceptor1的preHandle方法执行了)

 

2.MyInterceptor1和MyInterceptor2 的preHandle返回值为true

拦截器111111中的preHandler方法执行了..(MyInterceptor1的preHandle方法执行了)
拦截器22222中的preHandler方法执行了..(MyInterceptor2的preHandle方法执行了)
testInterceptor方法执行了...(controller方法执行)
拦截器22222中的postHandle方法执行了..(MyInterceptor2的postHandle方法执行了)
拦截器111111中的postHandle方法执行了..(MyInterceptor1的postHandle方法执行了)
拦截器22222中的afterCompletion方法执行了..(MyInterceptor2的afterCompletion方法执行了)
拦截器111111中的afterCompletion方法执行了..(MyInterceptor1的afterCompletion方法执行了)

 

3.MyInterceptor1返回值为true,MyInterceptor2 的preHandle返回值为false

拦截器111111中的preHandler方法执行了..(MyInterceptor1的preHandle方法执行了)
拦截器22222中的preHandler方法执行了..(MyInterceptor2的preHandle方法执行了)
拦截器111111中的afterCompletion方法执行了..(MyInterceptor1的afterCompletion方法执行了)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值