As a first into the internet industry, the small Xin Xin, in the use of SPRINGMVC found a good thing to play, decided to write down, lest the day forget, haha
First Kind
Serializing forms, serializing form data to JSON object strings
$ ("#submit"). Click (function () {var form=$ ("form"). Serializearray (); $.ajax ({url: "${ Pagecontext.request.contextpath}/teacher/updateteacher ", Data:form,type:" POST ", DataType:" JSON ", Error:function ( Data) {alert ("modified successfully"); Location.reload ();});});
The corresponding SPRINGMVC is going to have to take this data.
@RequestMapping (value= "Updateteacher") @ResponseBodypublic String updateteacher (Teacher Teacher,model Model) { Teacherservice.updatebyprimarykeyselective (teacher); Teacher Longinteacher = Teacherservice.loginexp (Teacher); Model.addattribute ("Loginteacher", Longinteacher); return " Success ";}
The second Kind
Building a JSON object
var zdgzs=[]; Get all the data on the page, assemble the JSON object for (Var i=0;i<qtcsid.length;i++) {var zdgz={"zdgznd": Zdgznd, "Rwlyvalue": Rwlyvalue, "RW Lyname ": Rwlyname," LWWH ": LWWH," LWBT ": LWBT," Qtcsid ": Qtcsid.eq (i). Val ()," Qtcsname ": Qtcsname.eq (i). html ()," Phcsid ": [Phcsid.eq (i). Val ()]," Phcsname ": [Phcsname.eq (i). html ()]," RWNR ": Rwnr.eq (i). Val ()}; Zdgzs.push (ZDGZ); }
Once again, the data for a Zdgz object is built.
Ajax Json.stringify (ZDGZS) Conversion of data, set the data type of the incoming background to JSON
$.ajax ({type: "POST", url: "${pagecontext.request.contextpath}/zdgz/addzdgz.do", ContentType: "Application/json; Charset=utf-8 ", Data:JSON.stringify (Zdgzs), DataType:" JSON ", success:function (message) {if (Message > 0) {alert (" Add Success "); window.location.href=" ${pagecontext.request.contextpath}/zdgz1/selectzdgz.do/s/0 ";}
Background receive mode, using the @requsetbody annotation in SPRINGMVC to control the parameter type, powerful SPRINGMVC can automatically assemble the foreground JSON data into the corresponding object's list array (need to add SPRINGMVC in the JSON package)
@RequestMapping (value= "/addzdgz.do", method = requestmethod.post) public @ResponseBody int Addzdgz (@RequestBody List <Zdgz> Zdgzs) {for (Zdgz Zdgz:zdgzs) {if ("Please select". Equals (Zdgz.getrwlyname ()) | | "Please select". Equals (Zdgz.getqtcsname ()) | | "Please select". Equals (Zdgz.getphcsname ()) | | ". Equals (Zdgz.getrwnr ()) | | Null = = ZDGZ.GETRWNR ()) {return 0;} ZDGZ.SETDTMDJSJ (New Date ()); Zdgzservice.addzdgz (ZDGZ);} return 1;}
OK, this is finished, you can look after yourself
This article is from the "entertain the Shell" blog, please be sure to keep this source http://10709011.blog.51cto.com/10699011/1771972
SPRINGMVC accepts AJAX submission forms, JSON data in two ways