【问题标题】:Displaying Data on a JSP page using Dojo Data grid使用 Dojo 数据网格在 JSP 页面上显示数据
【发布时间】:2012-03-22 05:25:59
【问题描述】:

我编写了这段代码,它返回一个 Json 字符串。它包含一组值(名称)。现在我想使用 Dojo 数据网格在 jsp 页面上显示这些值。我不知道如何使用这个返回的 Json 字符串作为 Dojo 网格的数据。以及如何格式化表格结构。我还希望当我单击表中的特定行(在这种情况下仅包含一个列 - 根据我的查询的员工姓名)时,会打开一个新窗口(可能是一个新的 JSP 页面)。怎么做?请帮我写代码。谢谢。

填充文本框.java

package MyPackage;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

public class PopulateTextbox {

    Gson gson = new Gson();
    String temp;
    List <String>rowValues = new ArrayList<String>();
    String[] contactListNames;
    Connection con=null;
    Statement st=null;
    ResultSet rs=null;


    public String method(){


        try{


        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        Class.forName(driver);

        String db = "jdbc:odbc:Practice_Database";
        con = DriverManager.getConnection(db,"","");

        st = con.createStatement();
        String sql = "SELECT Emp_Name FROM EmployeeSearch";
        rs = st.executeQuery(sql);

        while(rs.next()){

            rowValues.add(rs.getString("Emp_Name"));
        }
        contactListNames = (String[]) rowValues.toArray(new String[rowValues.size()]);
        temp = gson.toJson(contactListNames);

    }catch(Exception e){System.out.println(e);}
    /*finally{
        try {
                if(con!=null)con.close();
            } catch (SQLException e) {

                e.printStackTrace();
            }
        try {
            if(rs!=null)rs.close();
        } catch (SQLException e) {

            e.printStackTrace();
        }try {
            if(st!=null)st.close();

        } catch (SQLException e) {

            e.printStackTrace();
        }


    }*/
        return temp;

    }
}

【问题讨论】:

    标签: java json jsp datagrid dojo


    【解决方案1】:

    dojox.grid.DataGrid 使用数据存储作为其源。 dojo.data.ItemFileReadStore 可以接受一个 json 对象,即数据。

    var grid = ...
    var store = new dojo.data.ItemFileReadStore({
        data: YOUR_JSON_HERE
    });
    grid.setStore(store);
    

    store 用来初始化自身的 json 如下所示

    {
        identifier: 'id',
        items: [
            { id: 0, name: 'x' },
            { id: 1, name: 'y' }
        ]
    }
    

    所以你需要修改你的java代码来生成特定的json格式。另请注意,每件商品都有一个 ID,该 ID 在商店中的所有商品中必须是唯一的。

    http://dojotoolkit.org/reference-guide/dojo/data/ItemFileReadStore.html

    DataGrid 有 onRowClick 和 onRowDblClick 事件,当用户选择它时,您可以绑定它们来执行您需要的操作。

    http://dojotoolkit.org/reference-guide/dojox/grid/DataGrid.html#dojox-grid-datagrid

    【讨论】:

      猜你喜欢
      • 2015-10-05
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多