简体   繁体   中英

create a List of Map javascript?

Hello every one please how can get my array of objects created in javascript and send to spring controller.

$(table).find('tr').each(function(index) {
                        $this = $(this)
                        var value = $this.find("span.value").html();
                        var quantity = $this.find("input.quantity").val();
                        var lots = $this.find(".lots").val(), produits = $this
                                .find(".produits").val(), fournisseurs = $this
                                .find(".fournisseurs").val(), ref = $this
                                .find(".ref").html(), unite = $this
                                .find(".unite").val(), prix = $this
                                .find(".prix").html(), qte = $this
                                .find(".qte").val(), total = $this
                                .find(".total").text();
                        var ligne = new Object();
                        ligne.lot = lots;
                        ligne.produit = produits;
                        ligne.fournissuer = fournisseurs;
                        ligne.unite = unite;
                        ligne.prix = prix;
                        ligne.total = total;
                        ligne.ref = ref;
                        ligne.qte = qte;
                        tableau.push(ligne)

                    });

This is my Ajax request

 $.ajax({
        traditional : true,
        async : false,
        url : '/save/',
        contentType : "application/json",
        type : "POST",
        data : JSON.stringify(tableau),
        success : function(data) {
            console.log("okk ");
        },
        error : function(jqXHR, textStatus, errorThrown) {     
            console.log('erreur');
        }

And the Spring Controller.

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@RequestBody ArrayList<ChiffrageLigne> tableau) {
    log.info("liste des donnees = "+tableau.size());
    for (ChiffrageLigne chiffrageLigne : tableau) {
        System.out.println("ref fournisseur"+chiffrageLigne.getRefFour()+"prix ="+chiffrageLigne.getPrix());
    }
    return "ok";
}

So how i can get this array in sping mvc controller.Thanks

You can obtain List<Object> from Ajax rather List<ChiffrageLigne> without using ModelAttribute.

 $.ajax({
        traditional : true,
        async : false,
        url : '/save/',
        contentType : "application/json",
        type : "POST",
        dataType : "json",
        data : {tableau:JSON.stringify(tableau)},
        success : function(data) {
            console.log("okk ");
        },
        error : function(jqXHR, textStatus, errorThrown) {     
            console.log('erreur');
        }

Controller code

@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@RequestParam(value="tableau",required=true)  List<Object> tableauList) {
    //write code here converting List<object> to ArrayList<ChiffrageLigne>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM