使用Struts2和jQuery EasyUI实现简单CRUD系统(八)——Struts与EasyUI使用JSON进行交互

由于前面写了的四篇文章压缩得太厉害,还有真正的原理也没有弄通,所以重新写了使用Struts2和jQuery EasyUI实现简单CRUD系统(一)、(二)和(三)。

知道ajax与struts间用json交互后,那么EasyUI作为一个JQuery的UI插件集合体,JQuery为一个Javascript库,而ajax是异步的js和xml。JQuery的代码里面就是直接用了Ajax,EasyUI也是一样。

不同于《使用Struts2和jQuery EasyUI实现简单CRUD系统(五)——jsp,json,EasyUI的结合 》,由于前面用了struts的json插件,这次也将继续使用,不再做多一次页面跳转。

还有这次说明一下其中DataGrid的json格式要求。

直接参考《使用Struts2和jQuery EasyUI实现简单CRUD系统(三)——ajax,struts2使用json格式的交互

根据EasyUI直接写个表格页面,EasyUI_DataGrid.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Panel Tools - jQuery EasyUI Demo</title>
<link rel="stylesheet" type="text/css"
	href="js/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="js/themes/icon.css">
<link rel="stylesheet" type="text/css" href="js/demo/demo.css">
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.easyui.min.js"></script>
</head>
<body>
    <table id="dg" title="My Users" class="easyui-datagrid" style="width:550px;height:250px"
    		url="ajaxaction"
    		toolbar="#toolbar"
    		rownumbers="true"
    		fitColumns="true"
    		singleSelect="true">
    	<thead>
    		<tr>
    			<th field="name" width="50">Name</th>
    			<th field="phone" width="50">Phone</th>
    		</tr>
    	</thead>
    </table>
    <div id="toolbar">
    	<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">New User</a>
    	<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">Edit User</a>
    	<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">Remove User</a>
    </div>
</body>
</html>

根据官方教程——http://www.jeasyui.com/tutorial/app/crud.php中的php

$rs = mysql_query(‘select * from users‘);
$result = array();
while($row = mysql_fetch_object($rs)){
    array_push($result, $row);
}

echo json_encode($result);

大概可以推出使用java的action类该怎么传输数据,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<String,Object> map = new HashMap<String,Object>();
		map.put("name","xiaoming");
		map.put("phone","3349249");

		jsonresult = JSONObject.fromObject(map);
		System.out.println("json");

		return SUCCESS;
	}
	@Override
	public String execute() throws Exception {
		return null;
	}

	public JSONObject getJsonresult() {
		return jsonresult;
	}
	public void setJsonresult(JSONObject jsonresult) {
		this.jsonresult = jsonresult;
	}
}

这里将前面文章AjaxAction属性里面的类型改为JSONObject,因为String类型的时候,json串里面的字符串都用了转义字符\。

即使这样,我们会发现表格加载不出数据。

而且报的是Uncaught TypeError: Cannot read property ‘length‘ of undefined 错误,点击之后发现很奇怪的是,

看不出什么错,php又没有传输rows这样的字段。

搞了很久之后,我直接访问了view-source:http://www.jeasyui.com/tutorial/app/crud/get_users.php。

发现数据根本不是简单的json格式

{"total":"6","rows":[{"id":"26561","firstname":"1","lastname":"23","phone":"242","email":"[email protected]"},{"id":"26562","firstname":"2","lastname":"42","phone":"4231","email":"[email protected]"},{"id":"26563","firstname":"54345","lastname":"52","phone":"52","email":"[email protected]"},{"id":"26564","firstname":"42","lastname":"36","phone":"543","email":"[email protected]"},{"id":"26565","firstname":"42","lastname":"36","phone":"543","email":"[email protected]"},{"id":"26566","firstname":"54345","lastname":"52","phone":"52","email":"[email protected]"}]}

这不是明摆着坑人吗?

然后我直接下了demo的源码,打开php一看:

<?php
	$page = isset($_POST[‘page‘]) ? intval($_POST[‘page‘]) : 1;
	$rows = isset($_POST[‘rows‘]) ? intval($_POST[‘rows‘]) : 10;
	$offset = ($page-1)*$rows;
	$result = array();

	include ‘conn.php‘;

	$rs = mysql_query("select count(*) from users");
	$row = mysql_fetch_row($rs);
	$result["total"] = $row[0];
	$rs = mysql_query("select * from users limit $offset,$rows");

	$items = array();
	while($row = mysql_fetch_object($rs)){
		array_push($items, $row);
	}
	$result["rows"] = $items;
	echo json_encode($result);
?>

加了total和rows这些东西。但是官方文档并没有写好,就这样被坑了。现在反过来考虑一下如何将这样的json格式用java实现。

可以用Map放入total和rows,rows里面又是json,一个数据有两个字段name和phone,那么其实这样一个数据也需要用map存储,那么rows要将这么多map存起来,其实用list或者数组就可以了。

所以重新写一下AjaxAction类

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<String,Object> map = new HashMap<String,Object>();
		Map<String,Object> map2 = new HashMap<String,Object>();
		map.put("name","xiaoming");
		map.put("phone","3349249");

		map2.put("name","xiaoxin");
		map2.put("phone","3339589");

		list.add(map);
		list.add(map2);

		Map<String,Object> jsontemp = new HashMap<String,Object>();
		jsontemp.put("total",list.size());
		jsontemp.put("rows", list);
		jsonresult = JSONObject.fromObject(jsontemp);
		System.out.println("json");

		return SUCCESS;
	}
	@Override
	public String execute() throws Exception {
		return null;
	}

	public JSONObject getJsonresult() {
		return jsonresult;
	}
	public void setJsonresult(JSONObject jsonresult) {
		this.jsonresult = jsonresult;
	}
}

转换后的json格式会是:

{"total":2,"rows":[{"phone":"3349249","name":"xiaoming"},{"phone":"3339589","name":"xiaoxin"}]}

最后正常显示:

其实是官方的demo写的太简单,没意识对需要的数据格式。其实api里面也有说明:

Return the filtered data to display. The function take 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.

虽然网上例子很多,但是要真正搞懂为什么这样传参数,整个json与EasyUI之间是怎么交互的,全部理清之后,后续写起代码就很快了。

这回有代码了:

http://download.csdn.net/detail/iaiti/9120345

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-07 12:31:43

使用Struts2和jQuery EasyUI实现简单CRUD系统(八)——Struts与EasyUI使用JSON进行交互的相关文章

使用Struts2和jQuery EasyUI实现简单CRUD系统(转载汇总)

使用Struts2和jQuery EasyUI实现简单CRUD系统(一)——从零开始,ajax与Servlet的交互 使用Struts2和jQuery EasyUI实现简单CRUD系统(二)——ajax与struts2的交互 使用Struts2和jQuery EasyUI实现简单CRUD系统(三)——ajax,struts2使用json格式的交互 使用Struts2和jQuery EasyUI实现简单CRUD系统(四)——基础环境搭建 使用Struts2和jQuery EasyUI实现简单CRU

使用Struts2和jQuery EasyUI实现简单CRUD系统(五)——jsp,json,EasyUI的结合

这部分比較复杂,之前看过自己的同学开发一个选课系统的时候用到了JSON,可是一直不知道有什么用.写东西也没用到.所以没去学他.然后如今以这样的怀着好奇心,这是做什么用的,这是怎么用的.这是怎么结合的心态去学习,效果非常好. 这次用到的EasyUI的数据网格,DataGrid. 需用引用一个url传来的json数据.然后整齐美观地展如今页面上.想想自己之前做的东西.就直接拿数据库的数据和html的table代码进行拼接,整洁是整洁,可是代码写得特别别扭. 让我站在一个设计者的思路上来看,假设我如今

使用Struts2和jQuery EasyUI实现简单CRUD系统(二)——jsp,json,EasyUI的结合

这部分比较复杂,之前看过自己的同学开发一个选课系统的时候用到了JSON,但是一直不知道有什么用,写东西也没用到,所以没去学他.然后现在以这种怀着好奇心,这是做什么用的,这是怎么用的,这是怎么结合的心态去学习,效果很好. 这次用到的EasyUI的数据网格,DataGrid.需用引用一个url传来的json数据,然后整齐美观地展现在页面上.想想自己之前做的东西,就直接拿数据库的数据和html的table代码进行拼接,整洁是整洁,但是代码写得特别别扭.让我站在一个设计者的思路上来看,如果我现在提供了一

使用Struts2和jQuery EasyUI实现简单CRUD系统(四)——数据分页处理

上篇完成多选删除的功能之后,接下来就是做分页功能了.以前的分页是一个麻烦的问题.而且数据量巨大的时候,直接把这些元素取出来显然速度较慢,所以取一定区间的数据还是高效的. 之前自己写分页的时候,分页的界面当然是自己做的,用了ejui之后,真的方便了很多.方便到什么程度了. <table id="dg" title="My Users" class="easyui-datagrid" style="width: 700px; heig

使用Struts2和jQuery EasyUI实现简单CRUD系统(三)——复选框进行多选删除操作

继上篇的jsp,json,EasyUI的结合顺利将数据库数据转换成json格式后再EasyUI完成展示(Retrieve)之后,接下来的实现增删改功能也很顺手了. 增的功能还是一样,只不过将原本自己写的form用ejui提供form代替而已. 删改功能需要拿到具体选择行的id. <table id="dg" title="My Users" class="easyui-datagrid" style="width: 700px;

JQUERY插件JqueryAjaxFileUplaoder----更简单的异步文件上传

异步上传相信大家都做过类似的功能,JqueryAjaxFileUploader为我们提供了更简单的实现和使用方式.不过既然是JQUERY的插件那么它所依赖的环境大家都懂得.JqueryAjaxFileUploader并不华丽,也没有提供美化文件上传控件的css,它并不像jQuery File Upload(喜欢的同学可以去尝试下),提供了美观的样式和专门的图片预览.多任务上传等等, JqueryAjaxFileUploader 所拥有的很简单,只是异步上传文件的功能,当然这并不排除由你亲自为它披

Struts2 整合jQuery实现Ajax功能(1)

技术领域很多东西流行,自然有流行的道理,这几天用了jQuery,深感有些人真是聪明绝顶,能将那么多技术融合的如此完美. 首先明确个概念: jQuery是什么:是使用javascript语言开发的,用于满足项目前台各种操作需要的js程序文件.也就是说,jQuery基本上就是个js程序集,基础核心是jQuery.js文件. l        当然根据不同的版本具体的表现形式: jQuery.1.6.js或者jquery-1.5.1.js 这个是版本号的不同,具体有哪些区别,还没发现. l      

Struts2 整合jQuery实现Ajax功能(2)

1.1.1   Action利用struts2-json-plugin-X.X.X.jar响应Json格式信息: 1.      function removerecordbyid(recordid){ 2.              $("#showallrecord table tr").each( 3.              function(){ 4.                var seq=parseInt($( this ).children( "td&

EasyUI datagrid简单运用

jquery的前端框架挺多的,有easyUI ,bootstrap...,对于做系统软件或许easyUI比较好,因为里面控件很丰富,而bootstrap非常简洁大方,但是控件相 对比较少,特别是复杂的网格控件,几乎要自己来写! 介绍一下的easyUI 的datagrid,感觉写法还是比较简单易懂,重用性比较强!主要实现了显示数据,查询数据(序列化传参),datagrid分页样式选用. 页面如下: 例子依赖:1.asp.net mvc2.easyui文件依赖包    下载地址:http://www