2015-10-01 65 views
0

我想將json數據發送到網格extjs,但是我沒有連接到網格。另外url在我的網格代碼中似乎是錯誤的...任何想法?網格正在顯示,但語法正確。我知道url丟失,但是當我添加它時,數據不會被提取到網格中。發送json到網格

public class JsonForm extends HttpServlet { 

    public void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws IOException, ServletException 
    { 
     response.setContentType("text/html");  

     PrintWriter out = response.getWriter(); 

     JSONObject myObject = new JSONObject(); 
     myObject.put("firstname","Mike"); 
     myObject.put("lastname","J"); 
     myObject.put("email","[email protected]"); 
     out.println(myObject); 

     JSONObject myRecord = new JSONObject(); 
     myRecord.put("firstname","Mike"); 
     myRecord.put("lastname","J"); 
     myRecord.put("email","[email protected]"); 

     JSONArray myRecords = new JSONArray(); 
     myRecords.add(myRecord); 

    } 

} 



//grid 

Ext.onReady(function(){ 
    Ext.define('myRecord',{ 
     extend: 'Ext.data.Model', 
     proxy: { 
      type: 'memory', 
      reader: 'json' 
     }, 
     fields: [ 
      // set up the fields mapping into the xml doc 
      // The first needs mapping, the others are very basic 
      'firstName','lastName', 'email' 
     ] 
    }); 


    var gridStore = Ext.create('Ext.data.JsonReader', { 

     autoLoad: true, 
     proxy: { 
      // load using HTTP 
      type: 'ajax', 
      url: '', 
      // the return will be XML, so lets set up a reader 
      reader: { 
       type: 'json', 
       // records will have an "Item" tag 
       root: 'myRecord' 

      } 
     } 
    }); 


    grid = Ext.create('Ext.grid.Panel', { 
     store: gridStore, 
// selModel: sm, 
     columnLines: true, 
     frame: true, 
     columns: [ 
      {text: "First Name", flex:1, dataIndex: 'firstName', tdCls: 'no-dirty'}, 
      {text: "Last Name", flex:1, dataIndex: 'lastName', tdCls: 'no-dirty'}, 
      {text: "Email", flex:1, dataIndex: 'email', tdCls: 'no-dirty',} 

     ], 
     renderTo:Ext.getBody(), 
     width: '100%', 
     height: 650 
    }); 

}); 

回答

0

您的servlet代碼是否被執行?如果是,請查看從servlet收到的響應。可能json響應沒有正確形成。

1

gridStore應該是Ext.data.Store的實例。網址 - 應該是適當的網址到您的servlet。商店應該包含模型:'myRecord'。 Root屬性設置爲'myRecord',但正如我所看到的,根本沒有root屬性。