zoukankan      html  css  js  c++  java
  • SpringMVC=>Ajax

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
        <script src="statics/js/jquery-3.5.1.js"></script>
        <script>
            function a1() {
    
            $.post({
                url: "${pageContext.request.contextPath}/a3",
                data:{"name":$("#name").val()},
                success:function (data) {  //callback 回调函数
                    if (data.toString()==='ok'){
                        $("#userInfo").css("color","green");
                    }else{
                        $("#userInfo").css("color","red");
                    }
                    $("#userInfo").html(data);
                }
            });
            }
    
            function a2() {
                $.post({
                    url: "${pageContext.request.contextPath}/a3",
                    data:{"pwd":$("#pwd").val()},
                    success:function (data) {  //callback 回调函数
                        if (data.toString()==='ok'){
                            $("#pwdInfo").css("color","green");
                        }else{
                            $("#pwdInfo").css("color","red");
                        }
                        $("#pwdInfo").html(data);
                    }
                });
            }
        </script>
    </head>
    <body>
    <p>
        用户名: <input type="text" id="name" onblur="a1()">
        <span id="userInfo"></span>
    </p>
    <p>
        密码: <input type="text" id="pwd" onblur="a2()">
        <span id="pwdInfo"></span>
    </p>
    </body>
    </html>

     AjaxController

    package com.min.controller;
    
    import com.min.pojp.User;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    @RestController //加上这个会自动返回字符串
    public class AjaxController {
    
        @RequestMapping("/t1")
        public String test() {
            return "hello";
        }
    
    
        @RequestMapping("/a1")
        public void a1(String name, HttpServletResponse response) throws IOException {
            System.err.println("a1:param=>" + name);
            if ("666".equals(name)) {
                response.getWriter().print("true");
            } else {
                response.getWriter().print("false");
            }
        }
    
        @RequestMapping("/a2")
        public List<User> a2() { //接收 添加数据 返回数据
            List<User> userList = new ArrayList<User>();
            userList.add(new User("张三", 18, "男"));
            userList.add(new User("李四", 22, "男"));
            userList.add(new User("王五", 16, "男"));
            return userList;
        }
    
        @RequestMapping("/a3")
        public String a3(String name, String pwd) {
            String msg = "";
            if (name != null) {
                if ("admin".equals(name)) {
                    msg = "ok";
                }else{
                    msg = "用户名有误!";
                }
            }
            if (pwd != null) {
                if ("123456".equals(pwd)) {
                    msg = "ok";
                }else{
                    msg = "密码有误!";
                }
            }
            return msg;
        }
    
    }
  • 相关阅读:
    mybatis中的#和$的区别
    Java 导出 CSV
    java生成UUID
    Java并发编程的艺术(七)——Executors
    Java并发编程的艺术(六)——线程间的通信
    Java并发编程的艺术(五)——中断
    Java并发编程的艺术(四)——线程的状态
    Java并发编程的艺术(三)——volatile
    Java并发编程的艺术(二)——重排序
    Java并发编程的艺术(一)——并发编程需要注意的问题
  • 原文地址:https://www.cnblogs.com/m987/p/12901383.html
Copyright © 2011-2022 走看看