Recently in doing a small function module, the front desk has a lot of data need to be introduced to the background, the front page design is as follows:
It does not look very clear, in short, the data in the form is submitted to the background for processing, and then inserted into the database, and submitted to the background together, the implementation of the method is roughly two: (in dealing with similar needs, if you have other comments, you can share with each other)
1 through the form submission, of course, in the background, you need to define a collection object for each field, and then use the name flag in the page, if you use STRUT2, it is simpler, the collection object name defined in action and the label of the page label is the same. Then these data in the background can be achieved, here is not elaborated;
2 encapsulates the JSON object, then submits it to the background via an Ajax method, encapsulates each row of records as a JSON object, and then joins the JSON objects into an array, with the core code as follows:[JavaScript] View plain Copy//package JSON Array Object function getjsonobject () { var datearray=getarraybyname ("Plan_date"); var weekarray=getarraybyname ("Plan_week"); var jctypearray=getarraybyname ("JcType"); var jcnumarray=getarraybyname ("JcNum"); var xcxcarray=getarraybyname ("Xcxc"); var kilometrearray=getarraybyname ("kilometre"); var kcareaarray=getarraybyname ("Kcarea"); var commentsarray=getarraybyname ("comments"); var starttime=$ ("#startTime"). Val (); var endtime=$ ("#endTime"). Val (); var plan_title=$ ("#plan_title"). Val (); var objArray=[]; var obj=null; var jsonobj=new object (); JSONObj.start_time=startTime; JSONObj.end_time=endTime; if (Plan _title== "") { plan_title= Starttime+ "to" +endtime+ "overhaul plan"; } jsonobj.plan_title=plan_title; for (var i=0;i<datearray.length;i++) { obj=new object (); obj.planTime=dateArray[i]; obj.planWeek=weekArray[i]; obj.jcType=jcTypeArray[i]; obj.jcNum=jcNumArray[i]; obj.xcxc=xcxcArray[i]; obj.kilometre=kilometreArray[i]; obj.kcarea=kcareaarray[i]; obj.comments=commentsArray[i]; objarray.push (obj); } jsonobj.jsonstr=json.stringify (ObjArray); return JSONObj; }
The general idea is to encapsulate the data for each column field into an array, because the empty string is also required "", you can get the corresponding value based on the subscript, and then encapsulate the value of each row as an object, dynamically adding properties and corresponding values to the object, which is called the key/ Value form, where I get the form of jsonobj data roughly as follows:
{"Start_time": "2013-06-19",
"End_time": "2013-08-20",
"title": "2013-06-19 to 2013-08-20 overhaul plan",
"Jsonstr": [{"Jctype": "SS3", "jcnum": 1234, "comments": "Test1" ...},
{"Jctype": "DDD", "Jcnum": 2365, "comments": "Test2" ...} ....]
};
This encapsulation is encapsulated according to the requirements of the project, using AJAX to request action, sending the data past: [JavaScript] view plain copy $.ajax ({ type: "POST", url: "Planmanage!saveplan.do", data:JSONObj, datatype: "Text", success:function (data) { alert ( Data); } });
We receive in the background: [Java] view plain copy String starttime=request.getparameter ("start_time"); String endtime=request.getparameter ("End_time"); String plantitle=request.getparameter ("Plan_title"); String jsonstr=request.getparameter ("Jsonstr");
StartTime, Endtime, title are able to receive the corresponding data, because the JSON encapsulated data is the form of key-value pairs, when we receive jsonstr, we found that jsonstr displayed as NULL, Indicates that we cannot pass the array object data to the background via Ajax because it is not supported, so we need to use Jsonobj.jsonstr=json.stringify (Objarray), convert the array object to a string, and pass it to the background to receive the The information displayed after the background is received is a string information for multiple objects:
{"Jctype": "SS3", "jcnum": 1234, "comments": "Test1" ...},{"Jctype": "DDD", "Jcnum": 2365, "comments": "Test2" ...} ....
We can parse our string JSON data by using the corresponding class under the Org.json package:[Java] View Plain copy Jsonarray jsonarray=new jsonarray (JSONSTR); JSONObject jsonObject=null; mainplandetail mainplandetail=null; List< Mainplandetail> list=new arraylist<mainplandetail> (); for (Int i=0;i<jsonarray.length (); i++) { jsonobject=jsonarray.optjsonobject (i); mainplandetail=new mainplandetail (); mainplandetail.setplantime ( Jsonobject.optstring ("Plantime")); mainplanDetail.setplanweek (jsonobject.optstring ("Planweek")); mainplandetail.setjctype (jsonobject.optstring ("Jctype")); mainplandetail.setjcnum (jsonObject.optString ("Jcnum")); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;MAINPLANDETAIL.SETXCXC ( Jsonobject.optstring ("XCXC")); mainplandetail.setkilometre (jsonobject.optstring ("kilometre")); mainplandetail.setkcarea (jsonobject.optstring ("KcArea")); mainplandetail.setcomments (Jsonobject.optstring ("comments")); mainplAndetail.setiscash (0); Mainplandetail.setnum (i+1); list.add (mainplandetail); }
My logic is parsed after encapsulation for the object to do the corresponding logical processing, jsonobject.optstring ("Planweek")) and Jsonobject.getstring ("Planweek")) The difference is that the field is not present in optstring, and GetString will receive exception information. So that our encapsulated JSON object or JSON array can be passed through the foreground Ajax into the background;
Note: In IE test, found json.stringify incompatible IE6, 7, that is, IE6, 7 under the use will be an error: Can not find the JSON object, on the Internet to find relevant information, we need to introduce a package, json2.js, can be downloaded in my blog: http:// download.csdn.net/detail/harderxin/6735339
A detailed reference to the Json.stringify () method: Http://www.jb51.net/article/29893.htm
Expand:
Json.stringify (), add value (Object,array,string,number ...) Serialized as a JSON string
Json.parse (), parsing JSON data into JS native values
Tojson (), as the second parameter in json.stringify (function filter) supplemental
We can go and try.