springmvc拦截器-定义和配置

1.

package cn.itcast.ssm.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 HandlerInterceptor1 implements HandlerInterceptor {

	// 每处理
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		System.out.println("HandlerInterceptor1...preHandle");
		
		//return false表示拦截,不向下执行
		//return true表示放行
		return true;
	}

	// post处理
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		
		System.out.println("HandlerInterceptor1...postHandle");
		
	}

	// 后来完成
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
		System.out.println("HandlerInterceptor1...afterCompletion");
	}

}

package cn.itcast.ssm.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 HandlerInterceptor2 implements HandlerInterceptor {

	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		System.out.println("处理者拦截器 2...preHandle");
		
		//return false表示拦截,不向下执行
		//return true表示放行
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		
		System.out.println("处理者拦截器 2...postHandle");
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
		System.out.println("处理者拦截器 2...afterCompletion");
	}

}

package cn.itcast.ssm.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		//获取请求的url
		String url = request.getRequestURI();
		//判断url是否是公开 地址(实际使用时将公开 地址配置配置文件中)
		//这里公开地址是登陆提交的地址
		if(url.indexOf("login.action")>=0){
			//如果进行登陆提交,放行
			return true;
		}
		
		//判断session
		HttpSession session  = request.getSession();
		//从session中取出用户身份信息
		String username = (String) session.getAttribute("username");
		
		if(username != null){
			//身份存在,放行
			return true;
		}
		
		//执行这里表示用户身份需要认证,跳转登陆页面
		request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
		
		//return false表示拦截,不向下执行
		//return true表示放行
		return false;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		
		System.out.println("登录拦截器...postHandle");
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
		System.out.println("登录拦截器...afterCompletion");
	}

}


2.

	<!--拦截器 -->
	<mvc:interceptors>
		<!--多个拦截器,顺序执行 -->
		<!-- 登陆认证拦截器 -->
		<mvc:interceptor>
			<mvc:mapping path="/**"/>
			<bean class="cn.itcast.ssm.interceptor.LoginInterceptor"></bean>
		</mvc:interceptor>
		<mvc:interceptor>
			<!-- /**表示所有url包括子url路径 -->
			<mvc:mapping path="/**"/>
			<bean class="cn.itcast.ssm.interceptor.HandlerInterceptor1"></bean>
		</mvc:interceptor>
		<mvc:interceptor>
			<mvc:mapping path="/**"/>
			<bean class="cn.itcast.ssm.interceptor.HandlerInterceptor2"></bean>
		</mvc:interceptor>
	</mvc:interceptors>


3.二个拦截器

DEBUG [http-bio-80-exec-7] - DispatcherServlet with name 'springmvc' processing GET r
equest for [/springmvc_mybatis1216/items/queryItems.action]
DEBUG [http-bio-80-exec-7] - Looking up handler method for path /items/queryItems.act
ion
DEBUG [http-bio-80-exec-7] - Returning handler method [public org.springframework.web
.servlet.ModelAndView cn.itcast.ssm.controller.ItemsController.queryItems(javax.servl
et.http.HttpServletRequest,cn.itcast.ssm.po.ItemsQueryVo) throws java.lang.Exception]
DEBUG [http-bio-80-exec-7] - Returning cached instance of singleton bean 'itemsContro
ller'
DEBUG [http-bio-80-exec-7] - Last-Modified value for [/springmvc_mybatis1216/items/qu
eryItems.action] is: -1
处理者拦截器 1...preHandle
处理者拦截器 2...preHandle
null
DEBUG [http-bio-80-exec-7] - Creating a new SqlSession
DEBUG [http-bio-80-exec-7] - Registering transaction synchronization for SqlSession [
org.apache.ibatis.session.defaults.DefaultSqlSession@f174bd]
DEBUG [http-bio-80-exec-7] - Fetching JDBC Connection from DataSource
DEBUG [http-bio-80-exec-7] - Registering transaction synchronization for JDBC Connect
ion
DEBUG [http-bio-80-exec-7] - JDBC Connection [jdbc:mysql://localhost:3306/mybatis, Us
erName=root@localhost, MySQL-AB JDBC Driver] will be managed by Spring
DEBUG [http-bio-80-exec-7] - ==>  Preparing: SELECT items.* FROM items 
DEBUG [http-bio-80-exec-7] - ==> Parameters: 
DEBUG [http-bio-80-exec-7] - <==      Total: 3
DEBUG [http-bio-80-exec-7] - Releasing transactional SqlSession [org.apache.ibatis.se
ssion.defaults.DefaultSqlSession@f174bd]
DEBUG [http-bio-80-exec-7] - Transaction synchronization deregistering SqlSession [or
g.apache.ibatis.session.defaults.DefaultSqlSession@f174bd]
DEBUG [http-bio-80-exec-7] - Transaction synchronization closing SqlSession [org.apac
he.ibatis.session.defaults.DefaultSqlSession@f174bd]
DEBUG [http-bio-80-exec-7] - Returning JDBC Connection to DataSource
处理者拦截器 2...postHandle
处理者拦截器 1...postHandle
DEBUG [http-bio-80-exec-7] - Rendering view [org.springframework.web.servlet.view.Jst
lView: name '/items/itemsList'; URL [/WEB-INF/jsp//items/itemsList.jsp]] in Dispatche
rServlet with name 'springmvc'
DEBUG [http-bio-80-exec-7] - Added model object 'itemtypes' of type [java.util.HashMa
p] to request in view with name '/items/itemsList'
DEBUG [http-bio-80-exec-7] - Added model object 'itemsQueryVo' of type [cn.itcast.ssm
.po.ItemsQueryVo] to request in view with name '/items/itemsList'
DEBUG [http-bio-80-exec-7] - Added model object 'org.springframework.validation.Bindi
ngResult.itemsQueryVo' of type [org.springframework.validation.BeanPropertyBindingRes
ult] to request in view with name '/items/itemsList'
DEBUG [http-bio-80-exec-7] - Added model object 'itemsList' of type [java.util.ArrayL
ist] to request in view with name '/items/itemsList'
DEBUG [http-bio-80-exec-7] - Forwarding to resource [/WEB-INF/jsp//items/itemsList.js
p] in InternalResourceView '/items/itemsList'
处理者拦截器 2...afterCompletion
处理者拦截器 1...afterCompletion
DEBUG [http-bio-80-exec-7] - Successfully completed request


4.

DEBUG [http-bio-80-exec-10] - DispatcherServlet with name 'springmvc' processing GET 
request for [/springmvc_mybatis1216/items/queryItems.action]
DEBUG [http-bio-80-exec-10] - Looking up handler method for path /items/queryItems.ac
tion
DEBUG [http-bio-80-exec-10] - Returning handler method [public org.springframework.we
b.servlet.ModelAndView cn.itcast.ssm.controller.ItemsController.queryItems(javax.serv
let.http.HttpServletRequest,cn.itcast.ssm.po.ItemsQueryVo) throws java.lang.Exception
]
DEBUG [http-bio-80-exec-10] - Returning cached instance of singleton bean 'itemsContr
oller'
DEBUG [http-bio-80-exec-10] - Last-Modified value for [/springmvc_mybatis1216/items/q
ueryItems.action] is: -1
DEBUG [http-bio-80-exec-10] - Successfully completed request
DEBUG [http-bio-80-exec-10] - DispatcherServlet with name 'springmvc' processing POST
 request for [/springmvc_mybatis1216/login.action]
DEBUG [http-bio-80-exec-10] - Looking up handler method for path /login.action
DEBUG [http-bio-80-exec-10] - Returning handler method [public java.lang.String cn.it
cast.ssm.controller.LoginController.login(javax.servlet.http.HttpSession,java.lang.St
ring,java.lang.String) throws java.lang.Exception]
DEBUG [http-bio-80-exec-10] - Returning cached instance of singleton bean 'loginContr
oller'
处理者拦截器 1...preHandle
处理者拦截器 2...preHandle
处理者拦截器 2...postHandle
处理者拦截器 1...postHandle
处理者拦截器 3...postHandle
DEBUG [http-bio-80-exec-10] - Rendering view [org.springframework.web.servlet.view.Re
directView: name 'redirect:/items/queryItems.action'; URL [/items/queryItems.action]]
 in DispatcherServlet with name 'springmvc'
处理者拦截器 2...afterCompletion
处理者拦截器 1...afterCompletion
处理者拦截器 3...afterCompletion
DEBUG [http-bio-80-exec-10] - Successfully completed request
DEBUG [http-bio-80-exec-10] - DispatcherServlet with name 'springmvc' processing GET 
request for [/springmvc_mybatis1216/items/queryItems.action]
DEBUG [http-bio-80-exec-10] - Looking up handler method for path /items/queryItems.ac
tion
DEBUG [http-bio-80-exec-10] - Returning handler method [public org.springframework.we
b.servlet.ModelAndView cn.itcast.ssm.controller.ItemsController.queryItems(javax.serv
let.http.HttpServletRequest,cn.itcast.ssm.po.ItemsQueryVo) throws java.lang.Exception
]
DEBUG [http-bio-80-exec-10] - Returning cached instance of singleton bean 'itemsContr
oller'
DEBUG [http-bio-80-exec-10] - Last-Modified value for [/springmvc_mybatis1216/items/q
ueryItems.action] is: -1
处理者拦截器 1...preHandle
处理者拦截器 2...preHandle
null
DEBUG [http-bio-80-exec-10] - Creating a new SqlSession
DEBUG [http-bio-80-exec-10] - Registering transaction synchronization for SqlSession 
[org.apache.ibatis.session.defaults.DefaultSqlSession@fed5d4]
DEBUG [http-bio-80-exec-10] - Fetching JDBC Connection from DataSource
DEBUG [http-bio-80-exec-10] - Registering transaction synchronization for JDBC Connec
tion
DEBUG [http-bio-80-exec-10] - JDBC Connection [jdbc:mysql://localhost:3306/mybatis, U
serName=root@localhost, MySQL-AB JDBC Driver] will be managed by Spring
DEBUG [http-bio-80-exec-10] - ==>  Preparing: SELECT items.* FROM items 
DEBUG [http-bio-80-exec-10] - ==> Parameters: 
DEBUG [http-bio-80-exec-10] - <==      Total: 3
DEBUG [http-bio-80-exec-10] - Releasing transactional SqlSession [org.apache.ibatis.s
ession.defaults.DefaultSqlSession@fed5d4]
DEBUG [http-bio-80-exec-10] - Transaction synchronization deregistering SqlSession [o
rg.apache.ibatis.session.defaults.DefaultSqlSession@fed5d4]
DEBUG [http-bio-80-exec-10] - Transaction synchronization closing SqlSession [org.apa
che.ibatis.session.defaults.DefaultSqlSession@fed5d4]
DEBUG [http-bio-80-exec-10] - Returning JDBC Connection to DataSource
处理者拦截器 2...postHandle
处理者拦截器 1...postHandle
处理者拦截器 3...postHandle
DEBUG [http-bio-80-exec-10] - Rendering view [org.springframework.web.servlet.view.Js
tlView: name '/items/itemsList'; URL [/WEB-INF/jsp//items/itemsList.jsp]] in Dispatch
erServlet with name 'springmvc'
DEBUG [http-bio-80-exec-10] - Added model object 'itemtypes' of type [java.util.HashM
ap] to request in view with name '/items/itemsList'
DEBUG [http-bio-80-exec-10] - Added model object 'itemsQueryVo' of type [cn.itcast.ss
m.po.ItemsQueryVo] to request in view with name '/items/itemsList'
DEBUG [http-bio-80-exec-10] - Added model object 'org.springframework.validation.Bind
ingResult.itemsQueryVo' of type [org.springframework.validation.BeanPropertyBindingRe
sult] to request in view with name '/items/itemsList'
DEBUG [http-bio-80-exec-10] - Added model object 'itemsList' of type [java.util.Array
List] to request in view with name '/items/itemsList'
DEBUG [http-bio-80-exec-10] - Forwarding to resource [/WEB-INF/jsp//items/itemsList.j
sp] in InternalResourceView '/items/itemsList'
处理者拦截器 2...afterCompletion
处理者拦截器 1...afterCompletion
处理者拦截器 3...afterCompletion
DEBUG [http-bio-80-exec-10] - Successfully completed request


5.

6.

7.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值