2011-03-15 69 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列表没有发送给客户端,你只是在打印,但没有包含它作为回应。