• SpringMVC 11 Ajax验证用户名体验和拦截器


    11.1 Ajax验证用户名


    1. 写一个 login.jsp 这个页面
    <%--
      Created by IntelliJ IDEA.
      User: muqua
      Date: 2022/7/26
      Time: 19:04
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>登录验证title>
        <script src="${pageContext.request.contextPath}/static/js/jquery-3.6.0.js">script>
    head>
    <body>
    
    <p>
        用户名:<input type="text" id="name" onblur="a1()">
        <br>
        <span id="userInfo">span>
    p>
    <p>
        密码:<input type="text" id="pwd" onblur="a2()">
        <br>
        <span id="pwdInfo">span>
    p>
    
    
    <script>
        function a1(){
            $.post({
                url: "${pageContext.request.contextPath}/a3",
                data: {
                    "name": $("#name").val()
                },
                success: (res) => {
                    if(res === 'ok'){
                        $("#userInfo").css("color","green");
                    }else{
                        $("#userInfo").css("color","red");
                    }
                    $("#userInfo").html(res);
                }
            })
        }
    
        function a2(){
            $.post({
                url: "${pageContext.request.contextPath}/a3",
                data: {
                    "pwd": $("#pwd").val()
                },
                success: (res) => {
                    if(res === 'ok'){
                        $("#pwdInfo").css("color","green");
                    }else{
                        $("#pwdInfo").css("color","red");
                    }
                    $("#pwdInfo").html(res);
                }
            })
        }
    script>
    
    body>
    html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    1. 写 Controller
    @RequestMapping("/a3")
        public String a3(String name,String pwd){
            String msg = "";
            System.out.println(name + " || " + pwd);
            if(name != null){
                // admin 这些数据 应该在数据库中查
                if("admin".equals(name)){
                    msg = "ok";
                }else{
                    msg = "用户名有误";
                }
            }
            if(pwd != null){
                // admin 这些数据 应该在数据库中查
                if("123".equals(pwd)){
                    msg = "ok";
                }else{
                    msg = "密码有误";
                }
            }
            return msg;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    在这里插入图片描述


    11.2 拦截器

    SpringMVC 的处理拦截器 类似于 Servlet 开发中的 过滤器 Filter,用于 对 处理器进行预处理和后处理。开发者 可以自己定义一些拦截器来实现 特定的功能。

    过滤器和拦截器的区别:拦截器是基于AOP思想开发出来的具体应用。也就是说 它是切面编程的,不会对原有的 代码 有影响。

    拦截器还有一个最大的好处:它会自动的过滤掉 jsp、html、css、image、js 等一系列资源文件,不去做 拦截的访问。而只对 Controller 方法进行拦截!!!

    1. applicationContext.xml 中 配置 拦截器
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           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.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        
        <context:component-scan base-package="top.muquanyu.controller">context:component-scan>
    
        
        <mvc:default-servlet-handler>mvc:default-servlet-handler>
    
        
        <mvc:annotation-driven>
            
            <mvc:message-converters register-defaults="true">
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <constructor-arg value="UTF-8"/>
                bean>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="objectMapper">
                        <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                            <property name="failOnEmptyBeans" value="false"/>
                        bean>
                    property>
                bean>
            mvc:message-converters>
        mvc:annotation-driven>
    
        
        <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            
            <property name="prefix" value="/WEB-INF/jsp/">property>
            
            <property name="suffix" value=".jsp">property>
        bean>
        
    
        <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/**/"/>  
                <bean class="top.muquanyu.config.MyInterceptor">bean> 
            mvc:interceptor>
        mvc:interceptors>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    1. 写一个类 并且 实现 HandlerInterceptor 接口,就是一个拦截器了。

    preHandle:请求目标页面/Controller方法 处理前,触发拦截。
    postHandleController方法处理完之后,DispatcherServlet进行视图的渲染之前,也就是说在这个方法中你可以对ModelAndView进行操作
    afterCompletionDispatcherServlet进行视图的渲染之后,多用于清理资源。

    preHandle 的返回值 是 boolean 类型的,返回 true 则继续执行下一个拦截器 或 直接访问到页面。返回 false 则 卡住 不再往下执行。

    package top.muquanyu.config;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class MyInterceptor implements HandlerInterceptor {
    
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            System.out.println("============处理前============");
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
            System.out.println("============处理后============");
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
            System.out.println("============进行清理============");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    package top.muquanyu.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class TestController {
        @RequestMapping("/t1")
        public String test(){
            System.out.println("TestController==> test() 执行了!");
            return "OK";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    VSCode开启Pull Request更新代码分支可视化新篇章
    Oracle dblink 跨库查询详解
    C++一些新的特性的理解(二)
    MySQL数据类型相关内容
    Java Day1
    数字孪生10个技术栈:数据清洗-数据的洗衣机
    探讨 MyBatis 特殊 SQL 执行技巧与注意事项
    JavaScript 数据类型与运算符(上)
    git 常用命令总结
    猿创征文|我Java开发那些年陪我成长的工具清单
  • 原文地址:https://blog.csdn.net/qq_52606908/article/details/126001630