如何将JSON数据发送到Dojo数据网格?

时间:2014-08-12 06:19:18

标签: java json datagrid dojo

我编写了一个带有数据库连接的JSP页面,从数据库获取数据并打印出JSON格式。

现在我希望将JSON数据发送到Dojo数据网格

这是我的JSP代码。

    <html>
    <head>  
        <script type="text/javascript">
            dojo.require("dojox.data.QueryReadStore");
            dojo.require("dojox.grid._CheckBoxSelector");
            dojo.require("dojo.data.ObjectStore");
            dojo.require("dojo.parser");
            dojo.require("dojox.grid.DataGrid");
            dojo.require("dijit/form/Button");
            dojo.require("dojox/grid/DataGrid");
            dojo.require("dojo/data/ItemFileReadStore");
            dojo.ready(function(){
                mystore=new dojox.data.QueryReadStore({url:"/DojoProject/test"});
                grid = new dojox.grid.DataGrid({
                        store: mystore,
                        structure: [
                            {type: "dojox.grid._CheckBoxSelector",  editable: true},
                           [{name:"id", field:"ID", width: "200px",editable: true},
                            {name:"name", field:"name", width: "200px",editable: true},                         
                            {name:"Result", field:"Result", width: "200px"}]
                        ]}, dojo.byId("gridContainer"));                    
               grid.startup();
            });
        </script>
    <%
        JSONObject responseObj = new JSONObject();
        List<JSONObject> data = new LinkedList<JSONObject>();
        try {
            String dbms = "SQL SERVER"; 
            String serverName = "";
            String portNumber = "1106";
            String userName = "";
            String upassword = "";
            String databaseName = "";
            int datacount = 0;

            //String connectionURL = "jdbc:" + dbms + "://" + sN
            //      + ":" + PN + "/" + databaseN;
            Connection conn = null;
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection("");
            System.out.println("connection::" + conn);

            String strQuery = "select * from sample";
            System.out.println(strQuery);
            PreparedStatement st = conn.prepareStatement(strQuery);
            ResultSet rs = st.executeQuery();
            while(rs.next()) {
                datacount++;    
                JSONObject object = new JSONObject(); 
                object.put("s1",(String.valueOf(datacount)));
                object.put("ID",rs.getString(1));
                object.put("Name",rs.getString(2));
                System.out.println("ID&name&::"+rs.getString(1) +","+rs.getString(2));
            } 
            responseObj.put("data",data);    
            PrintWriter writer = response.getWriter();                           System.out.println({\"identifier\":\"data\",\"label\":\"data\",\"items\":"+responseObj+"}");       
            writer.write(" "+responseObj+"");        
            writer.flush();
            writer.close();    
        } catch (Exception e) {
            System.out.println(e);  
        }  %>  

    <head>
    <body class="claro">
        <div id="some">
            <div id="gridContainer" style="width: 100%; height: 200px;">
            <button data-dojo-type="dijit/form/Button" type="submit" name="submitButton" value="Submit">Submit</button> </div>   
        </div>
    </body>
</html>

在这里,我以JSON格式从数据库获取数据如下:

{"data":[{"sl":"1","id":"1","name":"shivasrinat"}
{"data":[{"sl":"1","id":"1","name":"shivasrinat"}

1 个答案:

答案 0 :(得分:0)

QueryReadStore需要来自JSP的特定JSON格式的数据,否则它不知道如何处理它。

查看QueryReadStore后端sample PHP implementation的最后一行:

print '/*'.json_encode(array('numRows'=>$numRows, 'items'=>$ret, 'identity'=>'id')).'*/';

它与您的格式没有太大区别,但您看到它需要3个特定的键:numRows,items和identity。你只有一个:数据。因此,尝试更改JSP以输出如下内容:

{"numRows":42, "identity":"id", "items": [{"sl":"1","id":"1","name":"shivasrinat"}] }

numRows属性应该是与查询匹配的可用项目总数(因为有时您只想检索少量数据,而不是整个数据库)。