SpringMvc 拦截器 Interceptor的基础知识

1.spring的拦截器和struts2的拦截器概念一样

2.实现拦截器

a)实现HandlerInterceptor接口

  

package cn.com.jit.intercepter;

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

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

public class MyIntercepter implements HandlerInterceptor{

	
	/**
	 * 在DispatchServlet后进行拦截(一般是进行一些清理工作)
	 */
	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("这是清理工作");
		
	}

	/**
	 * 在执行要执行的方法后进行拦截
	 */
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		
		System.out.println("-----------------method执行后执行的------------");
		
	}

	
	/**
	 * 在执行要执行的方法前进行拦截
	 * 返回true继续向下执行,返回false时,终止执行
	 */
	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2) throws Exception {
		// TODO Auto-generated method stub
		
		System.out.println("-----------------method执行前执行的------------");
		
		return true;
	}

}
b)配置拦截器

<?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:p="http://www.springframework.org/schema/p"     
        xmlns:context="http://www.springframework.org/schema/context" 
        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.0.xsd   
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
       
       
       <!-- 配置springMVC 注解驱动 -->
       <mvc:annotation-driven/>
       <!-- 扫描器 -->
       <context:component-scan base-package="cn"></context:component-scan>
       
       <span style="color:#ff0000;"><mvc:interceptors>
       		<mvc:interceptor>
       			<!-- 如果是/**包括路径及其子路径
       				如果是/admin/*  拦截的是/admin/add, /admin/list 这样的  ./admin/user/add这样的是不进行拦截的
       				如果是/admin/** 拦截的是 /admin/add, /admin/list, ./admin/user/add-->
       			<mvc:mapping path="/**"/>
       			<!-- 对应的拦截器 -->
       			<bean class="cn.com.jit.intercepter.MyIntercepter"></bean>
       		</mvc:interceptor>
       </mvc:interceptors></span>
       
</beans>

如果被拦截----怎么跳转到指定的界面?

可以使用重定向或者是请求转发到指定界面:

package cn.com.jit.intercepter;

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

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

public class MyIntercepter implements HandlerInterceptor{

	
	/**
	 * 在DispatchServlet后进行拦截(一般是进行一些清理工作)
	 */
	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("这是清理工作");
		
	}

	/**
	 * 在执行要执行的方法后进行拦截
	 */
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		
		System.out.println("-----------------method执行后执行的------------");
		
	}

	
	/**
	 * 在执行要执行的方法前进行拦截
	 * 返回true继续向下执行,返回false时,终止执行
	 */
	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse res,
			Object arg2) throws Exception {
		// TODO Auto-generated method stub
		
		System.out.println("-----------------method执行前执行的------------");
		
		<span style="color:#cc0000;">res.sendRedirect(req.getContextPath() + "/index.jsp");
		//req.getRequestDispatcher("index.jsp").forward(req, res);</span>
		System.out.println("-----------------method执行前执行的00------------");
		
		return false;
	}

}
3.实现登录拦截器:

a)实现的HandlerInterceptor接口:

package cn.com.jit.intercepter;

import java.util.List;

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

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

public class MyIntercepter implements HandlerInterceptor{

	private List<String> passurl;
	
	/**
	 * 在DispatchServlet后进行拦截(一般是进行一些清理工作)
	 */
	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("这是清理工作");
		
	}

	/**
	 * 在执行要执行的方法后进行拦截
	 */
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		
		System.out.println("-----------------method执行后执行的------------");
		
	}

	
	/**
	 * 在执行要执行的方法前进行拦截
	 * 返回true继续向下执行,返回false时,终止执行
	 */
	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse res,
			Object arg2) throws Exception {
		Object user = req.getSession().getAttribute("user");
		if(user != null)
			return true;
		String url = req.getRequestURL().toString();
		for(String temp:passurl){
			if(url.endsWith(temp)){
				return true;
			}
		}
		
		res.sendRedirect(req.getContextPath() + "/login.jsp");
		//req.getRequestDispatcher("index.jsp").forward(req, res);
		
		return false;
	}

	public List<String> getPassurl() {
		return passurl;
	}

	public void setPassurl(List<String> passurl) {
		this.passurl = passurl;
	}

}
b)实现拦截器配置:

<?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:p="http://www.springframework.org/schema/p"     
        xmlns:context="http://www.springframework.org/schema/context" 
        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.0.xsd   
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
       
       
       <!-- 配置springMVC 注解驱动 -->
       <mvc:annotation-driven/>
       <!-- 扫描器 -->
       <context:component-scan base-package="cn"></context:component-scan>
       
       <mvc:interceptors>
       		<mvc:interceptor>
       			<!-- 如果是/**包括路径及其子路径
       				如果是/admin/*  拦截的是/admin/add, /admin/list 这样的  ./admin/user/add这样的是不进行拦截的
       				如果是/admin/** 拦截的是 /admin/add, /admin/list, ./admin/user/add-->
       			<mvc:mapping path="/**"/>
       			<!-- 对应的拦截器 -->
       			<bean class="cn.com.jit.intercepter.MyIntercepter">
       				<property name="passurl">
       					<list>
       						<value>login.do</value>
       						<value>add.do</value>
       					</list>
       				</property>
       			</bean>
       		</mvc:interceptor>
       </mvc:interceptors>
       
</beans>
c)控制类:

package cn.com.jit.controller;

import javax.servlet.http.HttpSession;

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

import cn.com.jit.po.User;

@Controller
public class MyController {

	@RequestMapping("login.do")
	public String login(User user, HttpSession session){
		
		if(user.getUsername().equalsIgnoreCase("admin") && user.getPassword().equals("123")){
			session.setAttribute("user", user);
			return "index.jsp";
		}
		
		return "login.jsp";
	}
	
	@RequestMapping("add")
	public String test(){
		System.out.println("add");
		return "login.jsp";
		
	}
	
}








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值