【SSM】SpringMVC(三:SpringMVC拦截器)

文章目录

  • 1. 登录案例
  • 2. 拦截器
    • 2.1 应用
    • 2.2 拦截器的执行原理
    • 2.3 拦截器执行的时机
    • 2.4 拦截器的实现方法
    • 2.5 拦截器的实现步骤
    • 2.6 开发拦截器

1. 登录案例

【SSM】SpringMVC(三:SpringMVC拦截器)_第1张图片
【login.jsp】

<%--
  Created by IntelliJ IDEA.
  User: Beyong
  Date: 2023/4/17
  Time: 11:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>登录</h2>
<form action="${pageContext.request.contextPath}/login">
    姓名:<input name="name"/>
    密码:<input type="password" name="pwd"/>
    <input type="submit" value="登录"/>
</form>
${msg}
</body>
</html>

【main.jsp】

<%--
  Created by IntelliJ IDEA.
  User: Beyong
  Date: 2023/4/15
  Time: 21:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>main</title>
</head>
<body>
<h1>欢迎来到主页</h1>
</body>
</html>

【WebInfAction.java】

package com.sdnu.controller;

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

import javax.servlet.http.HttpServletRequest;

@Controller
public class WebInfAction {
    @RequestMapping("/main")
    public String main(){
        System.out.println("访问main.jsp");
        return "main";
    }
    @RequestMapping("/showLogin")
    public String showLogin(){
        System.out.println("访问login.jsp");
        return "login";
    }
    //登录逻辑判断
    @RequestMapping("/login")
    public String login(String name, String pwd, HttpServletRequest httpServletRequest){
        if("xy".equalsIgnoreCase(name) && "123".equalsIgnoreCase(pwd)){
            return "main";
        } else {
            httpServletRequest.setAttribute("msg", "用户名或者密码不正确");
            return "login";
        }
    }
}

【SSM】SpringMVC(三:SpringMVC拦截器)_第2张图片


【SSM】SpringMVC(三:SpringMVC拦截器)_第3张图片


【SSM】SpringMVC(三:SpringMVC拦截器)_第4张图片


2. 拦截器

2.1 应用

日志 权限 性能

2.2 拦截器的执行原理

【SSM】SpringMVC(三:SpringMVC拦截器)_第5张图片

2.3 拦截器执行的时机

1)preHandle():在请求被处理之前进行操作
2)postHandle():在请求被处理之后,但结果还没有渲染前进行操作,可以改变响应结果
3)afterCompletion:所有的请求响应结束后执行善后工作,清理对象,关闭资源

2.4 拦截器的实现方法

1)继承HandlerInterceptorAdapter的父类
2)实现HandlerInterceptor接口,实现的接口,推荐使用实现接口的方式

2.5 拦截器的实现步骤

1)改造登录方法,在session中存储用户的信息,用于进行权限的验证
2)开发拦截器的功能,实现HandlerInterceptor接口,重写preHandle()方法
3)在springmvc.xml中注册拦截器

2.6 开发拦截器

package com.sdnu.interceptor;

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

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

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断是否登陆过
        if(request.getSession().getAttribute("user") == null){
            //此时没有登陆过,返回到登录页面
            request.setAttribute("msg", "您还没有登录过,请重新登录");
            request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}

springmvc的配置文件中注册拦截器

    
    <mvc:interceptors>
        <mvc:interceptor>
            
            <mvc:mapping path="/**"/>
            
            <mvc:exclude-mapping path="/showLogin"/>
            <mvc:exclude-mapping path="/login"/>
            
            <bean class="com.sdnu.interceptor.LoginInterceptor"/>
        mvc:interceptor>
    mvc:interceptors>

你可能感兴趣的:(SSM,servlet,java,spring)