2011-03-15 49 views
0

我正在使用java struts加載數據庫通過json對象的值, 但這些值未填充到我的extjs網格中:我不斷收到一個空網格。問題使用json將數值加載到我的extjs網格

我在下面列入了我的代碼。

回到Home.jsp

一個按鈕會在那裏此頁面上。點擊getvalues.jsp應該會出現。 在getvalues.jsp的extgrid應與內容從數據庫

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> 
<!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=ISO-8859-1"> 
     <title>Insert title here</title> 
    </head> 
    <body> 
     <form action="getvalues.do"> 
      <input type="submit"></input> 
     </form> 
    </body> 
</html> 

即將被presen下面是我的Java代碼。我用我的數據庫中的值填充JSON對象。

public class Json extends Action { 
    @Override 
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 
     // TODO Auto-generated method stub 
     ArrayList<Employee> emp=new ArrayList<Employee>(); 
     Myservice serve=new Myservice(); 
     emp=serve.getemployeesservice(); 
     Iterator<Employee> empitr=emp.iterator(); 
     JSONArray json=new JSONArray(); 
     JSONObject JSONobj=new JSONObject(); 
     while(empitr.hasNext()){ 
      JSONObject jobj=new JSONObject(); 
      Employee empl=new Employee(); 
      empl=empitr.next(); 
      jobj.put("empid",empl.getEmpid()); 
      jobj.put("empname",empl.getEmpname()); 
      json.add(jobj); 
     } 
     JSONobj.put("employee",json); 
     System.out.println(JSONobj.toString()); 
     return mapping.findForward("success"); 
    } 
} 

getvalues.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<!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=ISO-8859-1"> 
     <title>Insert title here</title> 
     <link rel="stylesheet" type="text/css" href="css/ext-all.css"> 
     <script type="text/javascript" src="js/ext-base.js"></script> 
     <script type="text/javascript" src="js/ext-all.js"></script> 
     <script type="text/javascript"> 
     Ext.onReady(function() { 
      var store=new Ext.data.JsonStore({ 
       proxy:new Ext.data.HttpProxy({ 
        url:'http://localhost:8080/JsonExample/getvalues.do' 
       }), 
       reader:new Ext.data.JsonReader({ 
        root:'employee', 
        fields:['empid','empname'] 
       }) 
      }); 

      store.load(); 
      var recs=store.getRange(); 
      alert(recs.length); 

      var grid = new Ext.grid.GridPanel({ 
       title:'employee information', 
       columns: [{ 
        header:"employeeid", 
        width:100, 
        dataIndex:'empid', 
        sortable:true 
       },{ 
        header:"employeename", 
        width:100, 
        dataIndex:'empname', 
        sortable:true 
       }], 
       store: store, 
       width:300, 
       height:300, 
       renderTo:Ext.getBody() 
      }); 
     }); 
     </script> 
    </head> 
    <body> 
     hi 
    </body> 
</html> 

但值不被填充的某些原因。請幫我解決這個問題。

在此先感謝!

+3

爲什麼不根據[格式指導](http://stackoverflow.com/editing-help)編輯您的問題,然後我會看看它。現在這是一個混亂 – ChrisR 2011-03-15 09:21:00

+0

我認爲你有一個錯字的問題:什麼是上述js代碼中的「storetore」?另外,你得到的錯誤是什麼?你嘗試使用螢火蟲進行調試嗎?你在調用load方法時是否收到任何json? – 2011-03-15 10:16:41

+0

確實修復格式,這個問題看起來很可怕。你也有一個錯字(用Javascript)與「coloumns」。最後,您在瀏覽器中從http:// localhost:8080/JsonExample/getvalues.do中收到了什麼響應文本? – Tommi 2011-03-15 11:07:33

回答

0

我不擅長Java。但是,我懷疑你的JSON列表沒有發送給客戶端,你只是在打印,但沒有包含它作爲迴應。