qq_41403993
qq_41403993
采纳率100%
2018-10-17 01:39

ajax校验数据库数据是否存在

已采纳
 <!-- 查询student_id是否存在 -->
    <select id="getStudent_id" resultMap="studentMap" parameterType="int">
         SELECT * from student where student_id = #{student_id}
    </select>

@RequestMapping("/studentID")
    public Student getStudent_id(Integer student_id) {
        Student student = userService.getStudent_id(student_id);
        if (student==null){
            System.out.println("没有");
        }else{
            System.out.println("有");
        }
        return student;
    }



function check() {//js表单验证方法
        var text = document.getElementById("xh").value;//通过id获取需要验证的表单元素的值
        if (text == "") {
            alert("请输入学号!");//弹出提示
            return false;//返回false(不提交表单)
        }
        if (!(/(^[1-9]\d*$)/.test(text))) {
            alert("请输入学号!");//弹出提示
            return false;//返回false(不提交表单)
        }
        var student_id=$("#student_id").val();
        $.ajax({
           url:"<%=basePath%>user/studentID",
           type:"post",
           data:{"student_id":student_id},
           dataType:"json",

          success:function(data){
               if (data!=null ) {
                   alert("有");
            }else if(data==null && username !=''){
                alert("没有");
            }
               xmlhttp.open("GET","getcustomer.asp?q="+str,true);
               xmlhttp.send();
               //alert("啊啊啊");
          }

        });
        return true;//提交表单
    }
</script>
<body>
    <div class="listDIV">
        <table border="1" width="50%" height="50%" style="text-align: center;">
            <form action="<%=basePath%>user/one" method="post">
                <input type="text" id="xh" name="student_id" placeholder="根据学号查询">
                <button class="glyphicon glyphicon-select" onclick="return check();">查询</button>
            </form>

 严重: Servlet.service() for servlet [springMVC] in context with path [/StudentManagementSSM] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at com.zmk.controller.UserTestController.getStudent_id(UserTestController.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1139)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

我用ssm做的增删改查,搜索我想先校验一下数据库数据是否存在,存在就直接跳转查询,不存在就弹窗提示,中止提交表单,JS代码是我粘过来的,不会JS,有错误希望能告诉我如何改正,谢谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • qq_41403993 qq_41403993 3年前

    jsp

     function check() {//js表单验证方法
            var flag = true;
            var text = document.getElementById("xh").value;
            if (!(/(^[1-9]\d*$)/.test(text))) {//校验输入是否为正整数
                alert("请输入学号!");
                flag = false;
                //alert(flag);
            }
                var student_id = $('input[name="student_id"]').val();//获取input的值
                //alert(student_id);
                $.ajaxSetup({
                      async: false//设置同步
                      });
                $.post("<%=basePath%>user/queryByID", {student_id : student_id},function(data){
                    //alert(data);
                    if (!data) {//校验数据是否为空
                        alert("查无此人");
                        flag = false;
                        //alert(flag);
                    }
                });
            //alert("======"+flag);
            return flag;
        }
    

    controller

        @RequestMapping(value = "/queryByID", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
        public Student queryByID(Integer student_id, HttpServletRequest request) {//根据id校验数据是否存在
            Student student = userService.getStudent_id(student_id);
            /*try {
                if (student.getName() == null) {
                    System.out.println("111");
                } else {
                    System.out.println("有");
                    System.out.println(student_id);
                }
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
                System.out.println("没有");
            }*/
            return student;//将查询到的数据返回给ajax,没有返回null
        }
    
    
    点赞 评论 复制链接分享
  • qbian Qbian 3年前
    1. form表单
    <form action="<%=basePath%>user/one" method="post" onsubmit="return check(this)">
            <input type="text" id="xh" name="student_id" placeholder="根据学号查询">
            <button class="glyphicon glyphicon-select">查询</button>
    </form>
    
    1. js表单校验
    function check(form) {
        if (form.student_id.value == '') { 
            alert("查无此人"); 
            form.student_id.focus();
            return false;
        }
        return true;
    }
    
    1. java后端
    @Controller
    @RequestMapping("/user")
    public class Xxx{
    
        @RequestMapping("/one")
      public Student getStudent_id(Integer student_id) {
    
        }
    }
    
    点赞 评论 复制链接分享