java后台生成了json,前台却没有显示,为什么?
easyui吧
全部回复
仅看楼主
level 9
ILYXS2000 楼主
java:
@RequestMapping("findUserInfo")
@ResponseBody
public JSON findUserInfo() throws Exception {
JSONObject json = new JSONObject();
UserInfo userInfo = new UserInfo();
userInfo.setUserId("1");
userInfo.setUserName("a");
userInfo.setPassword("******");
userInfo.setBirthdayStr("20150101");
userInfo.setEmail("[email protected]");
List<UserInfo> userList = new ArrayList<UserInfo>();
userList.add(userInfo);
JSONArray userJson = JSONArray.fromObject(userInfo);
json.put("rows", userJson);
json.put("total", 1);
return json;
}
jsp:
<table id="dg" title="My Users" class="easyui-datagrid" style="width: 700px; height: 250px"
url="${context}/userInfo/findUserInfo.html" toolbar="#toolbar" pagination="true" rownumbers="true"
fitcolumns="true" singleselect="true">
<thead>
<tr>
<th field="userId" width="50">
用户ID
</th>
<th field="userName" width="50">
用户名
</th>
<th field="password" width="50">
密码
</th>
<th field="birthdayStr" width="50">
出生日期
</th>
<th field="email" width="50">
邮箱
</th>
</tr>
</thead>
</table>
debug已经走进去了,也生成出来json了。前台就是不显示,为什么?
2016年01月04日 07点01分 1
level 9
ILYXS2000 楼主
我找到原因了,
因为easyui的talbe中的url属性,支持的返回格式为json,我返回值中的json里的rows是dto型的list,而不是json型的list。
所以,JSONArray userJson = JSONArray.fromObject(userInfo);
json.put("rows", userJson);
这个根本没用。
2016年01月04日 09点01分 2
level 9
ILYXS2000 楼主
正确姿势如下:
JSONObject json = new JSONObject();
json.put("total", 1);
UserInfo userInfo = new UserInfo();
userInfo.setUserId("1");
userInfo.setUserName("a");
userInfo.setPassword("******");
userInfo.setBirthdayStr("20150101");
userInfo.setEmail("[email protected]");
List<UserInfo> userList = new ArrayList<UserInfo>();
userList.add(userInfo);
JSONObject userJson = new JSONObject();
userJson.put("userId", "1");
userJson.put("userName", "a");
userJson.put("password", "******");
userJson.put("birthdayStr", "20150101");
userJson.put("email", "[email protected]");
List<JSONObject> userInfoJsonList = new ArrayList<JSONObject>();
userInfoJsonList.add(userJson);
json.put("rows", userInfoJsonList);
return json;
2016年01月04日 09点01分 3
1