Using Ajax to pass an array on the front and back

Source: Internet
Author: User
Tags jquery form validation

We all know that JSON, as a data format compatible with both front and back ends, is well used for the transmission of complex data, especially asynchronous submissions. When we want to commit the front-end JS array to the backend and convert to a list, and then need to transfer the backend list to the front end of the conversion to a JS array, JSON as the intermediate transition format, is necessary. In the detailed summary below.

Programming Components Struts2, Fastjson, Ligerlistbox, Ajaxformsubmit.

First, the list data is passed to the front end to form a JS array

First, add a Menulistjson attribute to the bean that is passed to the front end, and convert the list data to a JSON string before passing it to this property.

String Menujson = json.tojsonstring (Accessprofile.getmenulist ()); Accessprofile.setmenulistjson (MenuJson);

Put Requestattribute so that the front-end jstl can get the bean

Servletactioncontext.getrequest (). SetAttribute ("Profile", accessprofile);

The front page can then convert the JSON to a JS array using the following syntax

var menulist = Json.parse (' ${profile.menulistjson} ');

second, the JS array is submitted to the backend and converted to a list object, where the table is submitted asynchronously, using the jquery form validation plugin

First we need to add a hidden tag to the <form></form> package, which is a private entity object defined in the Struts2 action, and matches the Get,set method, OGNL calls the Set method through the label's Name property to pay the value to the Menulistjson property.

<type= "hidden"  id= "Menulist"  name= " Profile.menulistjson "  value= '/>

The code for the AJAX submission section is as follows

        $ ("#form1"). Validate ({            function(form) {                $ (form). Ajaxsubmit ({                    beforesubmit: Showrequest,                    success:showresponse,                    ' <c:url value= ' domodify.action '/> ',                    ' json ' ,                    ' Post ',                    10000                });            }        );

The key point is that for the received JSON data (which represents the JS array), we use Fastjson's Parse method to convert to list

list<menutestbean> menulist=New arraylist<menutestbean> (Jsonarray.parsearray ( Profile.getmenulistjson (), Menutestbean. class ) );p rofile.setmenulist (menulist);

This completes the front-end conversion of the array.

Using Ajax to pass an array on the front and back

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.