如何在JSP控制器中接收从jQuery Post HTTP请求发送的数组对象数据

时间:2019-06-24 08:42:33

标签: jsp

我有多个数据需要存储到database,并且我的数据库中有两个表,一个budget_plan,另一个budget_plan_line,其中budget_plan_line取决于budget_plan ,那么我将在http request中使用存储对象的数组对象发送一个jquery,但我不知道如何接收这些数据并在我的JSP控制器中使用它们。

这是我的ajax http请求

let data = {
        action:action,
        code:pc.trim(),
        fin_year:fy,
        memo:memo.trim()
    };
    $.post("class/budget/budget_preparation_controller.jsp",data,function(datas){
        var data = JSON.parse(datas);           
        console.log(data);

        if(data.status){
            let rsl = data.result
            secondRequest(rsl)
        }else{
            notify(data.result, "danger")
        }
    }).fail(function(status,xhr){
        console.log(status)
    }).always(function(){
        console.log('End request')
    });

    }

    function secondRequest(rsl){
        let datas = [];
        let d = document.querySelectorAll("[data-amount]");
        for(let y = 0; y < d.length; y++){
            let re = d[y]
            let amount = re.value.trim();
            let activityId = re.getAttribute('data-amount');
            datas.push({
                id: activityId,
                amount: amount
            });
        }

        let body = {
            datas:datas,
            budget_plan_id:rsl.budget_plan_id
        }
        console.log(body);
        let sd = $.post("class/budget/budget_preparation_controller2.jsp",body,function(rs){

        });

        sd.fail(function(status,xhr){

        });

    #and this is my first JSP controller for the budget_plan

ResultSet resGetAllP = con.getrows(planTableName, getP);

        if (resGetAllP.next()){
                status_req.put("status",false);
                status_req.put("result", "the component is already recorded!");
                out.print(jsonData.toJson(status_req));
        }else{
            if (con.insert(planTableName, createP)){
                int id = 0;
                String sql = "select setval('budget_plan_id_seq', nextval('budget_plan_id_seq')-1)";
                Statement statement = conn.createStatement();
                ResultSet rows = statement.executeQuery(sql);
                if(rows.next()){
                   id = rows.getInt(1);
                }
                rows.close();
                status_req.put("status",true);
                createP.put("budget_plan_id",id);
                status_req.put("result",createP);
                out.print(jsonData.toJson(status_req));
            }
             else out.print("Sorry, Operation Failed! Pleasem try again later... ");
         }
        resGetAllP.close();

0 个答案:

没有答案