Use Struts2 and jQuery EasyUI to implement a simple CRUD system (8) -- interaction between Struts and EasyUI using JSON
Since the four articles previously written are compressed too much and the real principle is not reached, we re-wrote a simple CRUD System Using Struts2 and jQuery EasyUI (1), (2) and (3 ).
Knowing that ajax interacts with struts using json, EasyUI is a collection of JQuery UI plug-ins. JQuery is a Javascript library, while ajax is asynchronous js and xml. In JQuery code, Ajax is used directly, and the same is true for EasyUI.
Different from the use of Struts2 and jQuery EasyUI to implement a simple CRUD System (V)-jsp, json, and EasyUI combination, the struts json plug-in will be used this time, no more page jumps.
In addition, the json format requirements of the DataGrid are described here.
For more information, see use Struts2 and jQuery EasyUI to implement a simple CRUD System (iii) -- ajax and struts2 interaction in json format
Easyuidirectly writes a Table Page, easyui_datagrid.html
<Script type = text/javascript src = js/jquery. min. js> </script> <script type = text/javascript src = js/jquery. easyui. min. js> </script>
New User Edit User Remove User
According to the official tutorial -- PHP in http://www.jeasyui.com/tutorial/app/crud.php
$rs = mysql_query('select * from users');$result = array();while($row = mysql_fetch_object($rs)){ array_push($result, $row);} echo json_encode($result);
We can probably introduce how to transmit data using the java action class, AjaxAction. java
package action;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import net.sf.json.JSONObject;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.Action;public class AjaxAction implements Action{private JSONObject jsonresult;public String json() throws Exception {System.out.println(action execute);Map
map = new HashMap
();map.put(name,xiaoming);map.put(phone,3349249);jsonresult = JSONObject.fromObject(map);System.out.println(json);return SUCCESS;}@Overridepublic String execute() throws Exception {return null;}public JSONObject getJsonresult() {return jsonresult;}public void setJsonresult(JSONObject jsonresult) {this.jsonresult = jsonresult;}}
Here, we will change the type in the AjaxAction attribute of the previous article to JSONObject, because when the String type is used, the strings in the json String Use escape characters.
Even so, we will find that the table cannot load data.
The error "Uncaught TypeError: Cannot read property 'length' of undefined" is reported. It is strange to find that,
No error is found. php does not transmit fields such as rows.
After a long time, I directly accessed view-source: http://www.jeasyui.com/tutorial/app/crud/get_users.php.
The data is not in simple json format.
{total:6,rows:[{id:26561,firstname:1,lastname:23,phone:242,email:1@163.com},{id:26562,firstname:2,lastname:42,phone:4231,email:1@163.com},{id:26563,firstname:54345,lastname:52,phone:52,email:1@163.com},{id:26564,firstname:42,lastname:36,phone:543,email:1@163.com},{id:26565,firstname:42,lastname:36,phone:543,email:1@163.com},{id:26566,firstname:54345,lastname:52,phone:52,email:1@163.com}]}
Isn't that clear?
Then I directly go to the demo source code and open php to see:
Added total and rows. However, the official documents were not written, so they were pitted. Now, we will consider how to implement this json format in java.
You can use Map to put total and rows. rows contains json. A data has two fields: name and phone. In fact, such data also needs to be stored by map, then, rows needs to store so many maps. In fact, you can use list or array.
So rewrite the AjaxAction class.
package action;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import net.sf.json.JSONObject;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.Action;public class AjaxAction implements Action{private JSONObject jsonresult;public String json() throws Exception {System.out.println(action execute);List list = new ArrayList<>();Map
map = new HashMap
();Map
map2 = new HashMap
();map.put(name,xiaoming);map.put(phone,3349249);map2.put(name,xiaoxin);map2.put(phone,3339589);list.add(map);list.add(map2);Map
jsontemp = new HashMap
();jsontemp.put(total,list.size());jsontemp.put(rows, list);jsonresult = JSONObject.fromObject(jsontemp);System.out.println(json);return SUCCESS;}@Overridepublic String execute() throws Exception {return null;}public JSONObject getJsonresult() {return jsonresult;}public void setJsonresult(JSONObject jsonresult) {this.jsonresult = jsonresult;}}
The converted json format will be:
{total:2,rows:[{phone:3349249,name:xiaoming},{phone:3339589,name:xiaoxin}]}
Last normal display:
In fact, the official demo is too simple to write, not aware of the required data format. In fact, the api also describes:
Return the filtered data to display. the function takes one parameter 'data' that indicate the original data. you can change original source data to standard data format. this function must return standard data object that contain 'Total' and 'rows 'properties.
Although there are many examples on the Internet, it is necessary to understand why parameters are transmitted in this way and how the interaction between the entire json and EasyUI is realized. After all the information is clarified, the code will soon be written.