1.拦截器定义

Spring Web MVC 的处理器拦截器类似于Servlet 开发中的过滤器Filter,用于对处理器进行预处理和后处理。

2.拦截器demo

demo需求:

  1. 拦截用户请求,判断用户是否登录(登录请求不能拦截)
  2. 如果用户已经登录。放行
  3. 如果用户未登录,跳转到登录页面。

2.1 编写登录jsp页面

<%@ 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>Insert title here</title>
</head>
<body> <form action="${pageContext.request.contextPath }/user/login.action">
<label>用户名:</label>
<br>
<input type="text" name="username">
<br>
<label>密码:</label>
<br>
<input type="password" name="password">
<br>
<input type="submit"> </form> </body>
</html>

2.2 编写用户controller

@Controller
@RequestMapping("/user")
public class UserController { @RequestMapping("/toLogin")
public String toLogin() {
return "login";
} @RequestMapping("/login")
public String login(String username, String password, HttpSession session) {
// 校验用户登录
System.out.println(username);
System.out.println(password); // 把用户名放到session中
session.setAttribute("username", username); return "redirect:/item/itemList.action";
} }

2.3 编写拦截器

public class Interceptor1 implements HandlerInterceptor{

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
// 从request中获取session
HttpSession session = request.getSession();
// 从session中获取username
Object username = session.getAttribute("username");
// 判断username是否为null
if (username != null) {
// 如果不为空则放行
return true;
} else {
// 如果为空则跳转到登录页面
response.sendRedirect(request.getContextPath() + "/user/toLogin.action");
} return false;
} @Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { } @Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { } }

2.4 配置拦截器

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

<!-- SPringmvc的拦截器 -->
<mvc:interceptors>
<!-- 多个拦截器 -->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<!-- 自定义的拦截器类 -->
<bean class="cn.springmvc.interceptor.Interceptor1"/>
</mvc:interceptor> <!-- <mvc:interceptor>
<mvc:mapping path="/**"/>
自定义的拦截器类
<bean class="cn.springmvc.interceptor.Interceptor2"/>
</mvc:interceptor> -->
</mvc:interceptors>

最新文章

  1. 如何在ASP.NET MVC和EF中使用AngularJS
  2. cve-2015-5199漏洞分析
  3. ACM/ICPC 之 欧拉回路两道(POJ1300-POJ1386)
  4. Java 之 多线程编程
  5. postgresql中执行计划
  6. Happen-before
  7. Housse Robber II | leetcode
  8. JavaWeb基础: Web应用和Web服务器
  9. Maven--(一个坑)在settings.xml文件中添加mirrors导致无法新建Maven项目
  10. 【APP测试初体验】android测试命令----压力测试
  11. ASP.NET Core 处理 404 Not Found
  12. Python 内嵌函数运用(探究模块)
  13. nginx 编译参数详解(运维必看--转)
  14. [UNITY 5.4 UGUI] 模态对话框
  15. fastreport好象将想合并哪个单元就将那一列的TEXT控件的Merge的属性设成True就可以了
  16. Cwrsync_rsync windows_windows下的rsync
  17. jquery源码解析:pushStack,end,ready,eq详解
  18. docker 安装ElasticSearch head
  19. eclipse部署的web项目没有添加到Tomcat的webapps目录下解决方法
  20. App开发者博客之: 包建强 (专注移动app开发)

热门文章

  1. 给博客加入链接安全跳转页(添加一个和CSDN一样的链接跳转页)
  2. 简述LSM-Tree
  3. python-利用faker模块生成测试数据
  4. python 中的迭代器和生成器简单介绍
  5. go 中 sort 如何排序,源码解读
  6. .net core项目搭建swagger接口实现简单增删改查
  7. Windows安装face_recognition库
  8. Sqlserver 2008 导出数据库
  9. react 16.8版本新特性以及对react开发的影响
  10. 移动端开发为什么使用@2x@3x图片