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