表单提交由submit改为异步的方式

1,submit方式(页面会按action的url跳转,这个不方便一些交互)

function saveSingleYx(){
    $('#myTwoForm').submit();
}

2,ajax异步方式

function saveSingleYx(){
    //document.getElementById('myTwoForm').setAttribute("action", "saveSingleYx.ht");
     //$("myTwoForm").submit(function (event) {
         //event.preventDefault(); // 阻止表单的默认提交行为
         var formData =  $('#myTwoForm').serialize(); // 序列化表单数据                 
         $.ajax({
             url: "saveSingleYx.ht",
             type: "POST",
             data: formData,
             success: function (response) {
                 // 请求成功,处理后端返回的数据
                 console.log(response);
                 alert(response);
                 window.location.reload();
             },
             error: function () {
                 // 请求失败,处理错误情况
                 console.log("保存失败!!");
             }
         });
     //});
}

 

java后端

    @RequestMapping("saveSingleYx")
    @Action(description = "保存")
    @org.springframework.web.bind.annotation.ResponseBody
    public String saveSingleYx(HttpServletRequest request, HttpServletResponse response, Yjsydbsplc yjsydbsplc) throws Exception {
        String resString= "保存成功";try {
            List<Sydbyxsub> initSubAll = this.initSubAll(request);
            if (initSubAll!=null) {
                sydbyxsubService.batchUpdate(initSubAll);
            }else {
                logger.info("initSubAll=" + initSubAll);
            }
        } catch (Exception e) {
            resString  = "保存失败 请联系管理员!!"+e.getMessage();
        }
        return resString;
    }

 

    public List<Sydbyxsub> initSubAll(HttpServletRequest request) {

        List<Sydbyxsub> listSubAll = new ArrayList<Sydbyxsub>();

        // 所有子表id
        String[] idList = request.getParameterValues("subId");
        if (idList == null) {
            return null;
        }
        System.out.println("subId==" + idList.length);
        // 通过id 产生每个实例对象
        for (int i = 0; i < idList.length; i++) {
            Sydbyxsub sub = new Sydbyxsub();
            String idString = idList[i];
            if (idString != null && idString != "") {
                sub.setId(Long.parseLong(idString));
                listSubAll.add(sub);
            } else {
                System.out.println("有个subid为空" + idString);
            }
        }

        // 初始化实例对象的每个属性值
        String[] byzdList = request.getParameterValues("byzd");
        for (int i = 0; i < byzdList.length; i++) {
            if (byzdList[i] != null) {
                listSubAll.get(i).setByzd(byzdList[i]);
            }
        }
        String[] xhlxList = request.getParameterValues("xhlx");// 这个数量多了1,咋来的
        if (xhlxList != null) {
            // System.out.println(xhlxList.length);
            // System.out.println(listSubAll.size());
            for (int i = 0; i < xhlxList.length; i++) {
                if (xhlxList[i] != null) {
                    listSubAll.get(i).setXhlx(xhlxList[i]);
                }
            }
        }

        String[] ztList = request.getParameterValues("zt");

        if (ztList != null) {
            System.out.println("ztList=" + ztList.length);
            System.out.println("listSubAll=" + listSubAll.size());
            for (int i = 0; i < ztList.length; i++) {
                if (ztList[i] != null) {
                    listSubAll.get(i).setZt(ztList[i]);
                }
            }
        }

        String[] wjList = request.getParameterValues("wj");
        // logger.info("wjList="+wjList.length);
        for (int i = 0; i < wjList.length; i++) {
            if (wjList[i] != null) {
                listSubAll.get(i).setWj(wjList[i]);
            }
        }
        // String[] yxList = request.getParameterValues("yx");
        // for (int i = 0; i < yxList.length; i++) {
        // listSubAll.get(i).setYx(yxList[i]);
        // }
        String[] yxmcList = request.getParameterValues("yxmc");
        for (int i = 0; i < yxmcList.length; i++) {
            if (yxmcList[i] != null) {
                listSubAll.get(i).setYxmc(yxmcList[i]);
            }
        }
        String[] sfhbList = request.getParameterValues("sfhb");
        for (int i = 0; i < sfhbList.length; i++) {
            if (sfhbList[i] != null) {
                listSubAll.get(i).setSfhb(sfhbList[i]);
            }
        }
        String[] lbList = request.getParameterValues("lb");
        for (int i = 0; i < lbList.length; i++) {
            if (lbList[i] != null) {
                listSubAll.get(i).setLb(lbList[i]);
            }
        }
        String[] czmcList = request.getParameterValues("cz");
        for (int i = 0; i < czmcList.length; i++) {
            if (czmcList[i] != null) {
                listSubAll.get(i).setCz(czmcList[i]);
            }
        }
        String[] gjdjList = request.getParameterValues("gjdj");
        for (int i = 0; i < gjdjList.length; i++) {
            if (gjdjList[i] != null) {
                listSubAll.get(i).setGjdj(gjdjList[i]);
            }
        }
        String[] sfgzpList = request.getParameterValues("sfgzp");
        for (int i = 0; i < sfgzpList.length; i++) {
            if (sfgzpList[i] != null) {
                listSubAll.get(i).setSfgzp(sfgzpList[i]);
            }
        }
        String[] jxList = request.getParameterValues("jx");
        for (int i = 0; i < jxList.length; i++) {
            if (jxList[i] != null) {
                listSubAll.get(i).setJx(jxList[i]);
            }
        }
        String[] sfykList = request.getParameterValues("sfyk");
        for (int i = 0; i < sfykList.length; i++) {
            if (sfykList[i] != null) {
                listSubAll.get(i).setSfyk(sfykList[i]);
            }
        }
        String[] xhoneList = request.getParameterValues("xhone");
        for (int i = 0; i < xhoneList.length; i++) {
            if (xhoneList[i] != null) {
                listSubAll.get(i).setXhone(xhoneList[i]);
            }
        }
        String[] jgList = request.getParameterValues("jg");
        for (int i = 0; i < jgList.length; i++) {
            if (jgList[i] != null) {
                listSubAll.get(i).setJg(jgList[i]);
            }
        }
        String[] jgmcList = request.getParameterValues("jgmc");
        for (int i = 0; i < jgmcList.length; i++) {
            if (jgmcList[i] != null) {
                listSubAll.get(i).setJgmc(jgmcList[i]);
            }
        }
        String[] hbhxhList = request.getParameterValues("hbhxh");
        for (int i = 0; i < hbhxhList.length; i++) {
            if (hbhxhList[i] != null) {
                listSubAll.get(i).setHbhxh(hbhxhList[i]);
            }
        }
        String[] xxlxmcList = request.getParameterValues("xxlxmc");
        for (int i = 0; i < xxlxmcList.length; i++) {
            if (xxlxmcList[i] != null) {
                listSubAll.get(i).setXxlxmc(xxlxmcList[i]);
            }
        }
        String[] gfxxmsList = request.getParameterValues("gfxxms");
        for (int i = 0; i < gfxxmsList.length; i++) {
            if (gfxxmsList[i] != null) {
                listSubAll.get(i).setGfxxms(gfxxmsList[i]);
            }
        }
        String[] sfznzList = request.getParameterValues("sfznz");
        for (int i = 0; i < sfznzList.length; i++) {
            if (sfznzList[i] != null) {
                listSubAll.get(i).setSfznz(sfznzList[i]);
            }
        }
        String[] xhList = request.getParameterValues("myXh");
        for (int i = 0; i < xhList.length; i++) {
            if (xhList[i] != null && xhList[i] != "") {
                listSubAll.get(i).setXh(Long.parseLong(xhList[i]));
            }
        }
        String[] dhList = request.getParameterValues("dh");
        for (int i = 0; i < dhList.length; i++) {
            if (dhList[i] != null) {
                listSubAll.get(i).setDh(dhList[i]);
            }
        }
        String[] hbztList = request.getParameterValues("hbzt");
        for (int i = 0; i < hbztList.length; i++) {
            if (hbztList[i] != null) {
                listSubAll.get(i).setHbzt(hbztList[i]);
            }
        }
        return listSubAll;
    }
initSubAll

 

posted @ 2023-10-07 14:20  爱跳舞的程序员  阅读(67)  评论(0编辑  收藏  举报