JQuery Ajax delivers JSON arrays to spring Controller

Source: Internet
Author: User

JQuery Ajax is an easy way to pass a single JSON object to the background, where a JSON array with multiple JSON objects is passed into the Java background and explains how the JSON array is parsed by Java.

1. JS Code

var relationarrays=new Array ();
Get people information for all groups grid data
var allgrid= $ (". UserGrid");
for (Var i=0;i<allgrid.length;i++) {
var rows=$ (Allgrid[i]). DataGrid ("GetRows");
$.each (Rows,function (I,item) {
Relationarrays.push (item);
})
}

$.ajax ({
Type: "POST",
URL: '.. /projectcontroller/addrelations ',
data:{"params": Json.stringify (Relationarrays)},
DataType: ' JSON ',
Cache:false,
Success:function (data) {
alert (data.msg);
}
});

2. Java code

@RequestMapping ("/addrelations")
public void Addrelations (httpservletrequest request,httpservletresponse Response, HttpSession session) {
String jsonstr = Request.getparameter ("params");
Store project personnel relationship information that requires insert
List<projectrelation> relationlist=new arraylist<projectrelation> ();

Projectrelation Relation=null;
Jsonarray Jsonarray = Jsonutil.parsearray (JSONSTR);
for (Object Ob:jsonarray) {
Jsonobject jobject = (jsonobject) ob;
Relation=new projectrelation ();
Relation.setprojectid (PID);
Relation.setchargemanid (Jobject.getinteger ("Chargemanid"));
Relation.setgroupid (Jobject.getinteger ("groupId"));
Relation.setuserid (Jobject.getinteger ("userId"));
Relation.setprojectrole (Jobject.getinteger ("Projectrole"));
Relationlist.add (relation);
}
Check all the existing personnel information in the project first,
int result=projectserviceimpl.saveprojectrelations (relationlist);

hashmap<string, object> map = new hashmap<string, object> ();
try {
if (Result==jsonarray.size ()) {
Map.put ("msg", "Associated information added successfully");
}
else {
Map.put ("msg", "Associated Information add error");
}
Writejsonutil.writejson (map, response);
} catch (Exception e) {
E.printstacktrace ();
Map.put ("msg", "Associated Information add error");
Writejsonutil.writejson (map, response);
}
}

3. Jsonutil Code

public static Jsonarray Parsearray (String text) {
Jsonarray Jsonarray=json.parsearray (text);

return jsonarray;
}

Please download the specific jsonutil code from this link Http://files.cnblogs.com/files/DylanZ/JsonUtil.rar

JQuery Ajax delivers JSON arrays to spring Controller

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.