ext json请求java后台返回集合数据

[size=x-large]JAVA后台类文件:JsonUtil.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonUtil {
public static void main(String[] args) {
JsonUtil j = new JsonUtil();
//j.bean2json();
j.list2json();
//j.map2json();
//j.json2bean();
}

public JSONArray arr2json() {
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray = JSONArray.fromObject(boolArray);
System.out.println(jsonArray);

// prints [true,false,true]
return jsonArray;
}

public JSONObject list2json() {
List list = new ArrayList();
//list.add("first");
//list.add("second");

Map map1 = new HashMap();
map1.put("name", "json");
map1.put("sex", "男");


Map map2 = new HashMap();
map2.put("name", "json222");
map2.put("sex", "男222");

list.add(map1);
list.add(map2);

Map map = new HashMap();
map.put("root", list);
JSONObject json = JSONObject.fromObject(map);

System.out.println(json);
//JSONArray jsonArray = JSONArray.fromObject(list);
// prints ["first","second"]

return json;
}

public void createJson() {
JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']");
System.out.println(jsonArray);
// prints ["json","is","easy"]
}

public JSONObject map2json() {
Map map = new HashMap();
map.put("name", "json");
map.put("sex", "男");
map.put("bool", Boolean.TRUE);
map.put("int", new Integer(1));
map.put("arr", new String[] { "a", "b" });
map.put("func", "function(i){ return this.arr[i]; }");

JSONObject json = JSONObject.fromObject(map);
System.out.println(json);
// prints
// ["name":"json","bool":true,"int":1,"arr":["a","b"],"func":function(i){
// return this.arr[i]; }]
return json;
}

public JSONObject bean2json() {
JSONObject jsonObject = JSONObject.fromObject(new MyBean());
System.out.println(jsonObject);
/*
* prints
* {"func1":function(i){ return this.options[i];
* },"pojoId":1,"name":"json","func2":function(i){ return
* this.options[i]; }}
*/
return jsonObject;
}

public Object json2bean() {
String json = "{name=\"json2\",func1:true,pojoId:5,func2:function(a){ return a; }}";
JSONObject jb = JSONObject.fromObject(json);
MyBean bean = (MyBean)JSONObject.toBean(jb, MyBean.class);
return bean;
}

############grid.jsp

<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>Stateful Array Grid Example</title>

<!-- ** CSS ** -->
<!-- base library -->
<link rel="stylesheet" type="text/css" href="ext/resources/css/ext-all.css" />

<!-- overrides to base library -->

<!-- page specific -->
<link rel="stylesheet" type="text/css" href="ext/examples/shared/examples.css" />
<link rel="stylesheet" type="text/css" href="ext/examples/grid-examples.css" />

<style type=text/css>
/* style rows on mouseover */
.x-grid3-row-over .x-grid3-cell-inner {
font-weight: bold;
}

/* style for the "buy" ActionColumn icon */
.x-action-col-cell img.buy-col {
height: 16px;
width: 16px;
background-image: url(../shared/icons/fam/accept.png);
}

/* style for the "alert" ActionColumn icon */
.x-action-col-cell img.alert-col {
height: 16px;
width: 16px;
background-image: url(../shared/icons/fam/error.png);
}
</style>
<script type="text/javascript" src="ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="ext/ext-all.js"></script>

</head>
<body>
<script>
Ext.onReady(function(){
var store = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({
url:'AjaxServlet',
method: 'POST'
}),
reader: new Ext.data.JsonReader({
root: 'root',
id: 'name'
},[{name: 'name'},{name: 'sex'}]
)
});
store.load();

var column = new Ext.grid.ColumnModel([
{header: '姓名', dataIndex: 'name', width: 100},
{header: '性别', dataIndex: 'sex', width: 50}
]);
column.defaultSortable = true;

var grid = new Ext.grid.GridPanel({
el: 'grid-example',
width:700,
height:500,
title:'用户表',
store: store,
cm: column,
trackMouseOver: false,
sm: new Ext.grid.RowSelectionModel(),
bbar: new Ext.PagingToolbar({
pageSize: 25,
store: store,
displayInfo: true,
displayMsg: ' 本页显示从{0} 到 {1} 条,共 {2} 条数据',
emptyMsg: "没有可以显示的数据"
})
});
grid.render();

});
// grid.render('grid-example');
</script>
<div id="grid-example"></div>
</body>
</html>


###########AjaxServlet

package com.xiao.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.xiao.ajax.JsonUtil;

public class AjaxServlet extends HttpServlet {

/**
* Constructor of the object.
*/
public AjaxServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("SERVLET DOGET 方法被访问------------");
doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("SERVLET DOPOST 方法被访问------------");

String name=request.getParameter("name");
String password=request.getParameter("password");

System.out.println("获取提交参数:"+name+"--"+password);
JsonUtil json=new JsonUtil();
JSONObject jsonObj = json.list2json();

String str=jsonObj.toString();
System.out.println("返回给页面的JSON:"+str);

//response.setContentType("text/html");
response.setContentType("application/x-json;charset=GBK");
PrintWriter out = response.getWriter();
out.println(str);
out.flush();
out.close();
}

public void init() throws ServletException {
// Put your code here
}

}[/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值