<%@ 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>
1.先通过地址访问 InterceptorController控制器类
package com.itheima.web.controller;
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("登录了,已经访问到了");
return "success";
}
}
2.因为拦截器拦截url带有/springmvc的地址请求,所以执行InterceptorController 类之前,会先通过自定义的拦截器方法,进行判断是否放行
package com.itheima.web.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;
/**
* 自定义拦截器
* @author ChonZ
*
*/
public class CheckLoginInterceptor implements HandlerInterceptor {
/**
* 头部执行
*
* 如何调用:
* 拦截器