日期:2014-05-16  浏览次数:20750 次

请教:extjs的grid如何得到后台struts传来的JSON数据 - Web 开发 / Ajax
请教,刚用ext。我根据网上的例子,自己写了,前后台代码,但是gridpanel中总显示不出数据。。
前台是:
JScript code

Ext.onReady(function() {
    Ext.QuickTips.init();

    var note = Ext.data.Record.create([
        {name: 'nodeId'},   
        {name: 'title'},
        {name: 'context'}
    ]);
    
    var ds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({   
            url: "listNote.action",
            method:"POST"
        }),
        render:new Ext.data.JsonReader({
            totalProperty:"results",
            root:"rows",
            id:"nodeId"
        },note)
    });  
    ds.setDefaultSort('nodeId', 'desc');
    [color=#FFFF00]ds.load();[/color]
.....


在struts.xml中定义是
XML code

    <package name="note" extends="json-default">
        <action name="listNote" class="noteAction" method="jsonExecute">
       <result type="json"></result> 
        </action>
    </package>


在spring中定义我就不贴了。

后台action类中定义是
public String jsonExecute() throws Exception {
JSONArray jarray = new JSONArray();
JSONObject job = null;
  try{
List list = service.findAll();
Iterator i = list.iterator();

while (i.hasNext())
{
job = new JSONObject();
Object[] obj = (Object[]) i.next();
job.put("nodeId", obj[0]);
job.put("title", obj[1]);
job.put("context", obj[2]);
jarray.add(job);
}
}catch(Exception e){
job.put("success", false);
job.put("error", e.getMessage());
return ERROR;
}
this.setTotalCount(4);
  String allStudentsWithJson = "{\"results\":" + this.getTotalCount()  
  + ",\"rows\":";
   
allStudentsWithJson += jarray.toString();
allStudentsWithJson += "}";
outputResult(allStudentsWithJson);
return NONE;  
  }
  /**
  * 
  * 向浏览器输入json数据串
  *
  *@param json数据串
  *@return
  */
  public void outputResult(String result) {  
  HttpServletResponse response = ServletActionContext.getResponse();  
  
  try {  
  System.out.println(result);
  response.setContentType("text/json; charset=gb2312"); 
  response.getWriter().println(result);
  } catch (IOException e) {  
  // TODO Auto-generated catch block  
  e.printStackTrace();  
  
  }  
  
  }
action中返回的json数据格式是:
{"results":4,"rows":[{"nodeId":"1","title":"11","context":"22"},{"nodeId":"2","title":"11","context":"22"},{"nodeId":"3","title":"11","context":"22"},{"nodeId":"4","title":"11","context":"22"}]}
在gridPanel中显示不出json数据是怎么回事。。。




------解决方案--------------------