SPRINGMVC accepts AJAX submission forms, JSON data in two ways

Source: Internet
Author: User

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

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.