0

我有一个类,其中我有一个方法名称 method() 以 JSON 字符串的形式返回一个字符串。输出是::

[{"ID":1,"Names":"Shantanu"},{"ID":2,"Names":"Mayur"},{"ID":3,"Names":"Rohit"},{"ID":4,"Names":"Jasdeep"},{"ID":5,"Names":"Rakesh"}]

现在这是我想使用 Dojo 为 Datagrid 填充的数据形式。在我的 jsp 页面中,我按如下方式使用它::

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="MyPackage.PopulateTextbox" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
@import "http://localhost:8080/2_8_2012/js/dojo/resources/dojo.css";

@import "http://localhost:8080/2_8_2012/js/dijit/themes/nihilo/nihilo.css";
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/dojo/1.4/dojo/dojo.xd.js" djConfig="isDebug: false, parseOnLoad: true"></script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">

dojo.require("dojox.grid.DataGrid");
dojo.require("dojo.data.ItemFileReadStore");
</script>

<script type="text/javascript">
var temp1;
$(document).ready(function() {

PopulateTextbox obj = new PopulateTextbox();
temp1 = obj.method();
});
out.println(temp1);
var storedata={
        identifier:"table1",
        label:"name",
        items: temp1
}
var gridStructure =[{
cells:[
            [
                  { field: "ID",
                        name: "ID_Emp",
                        width: "40%", styles: 'text-align: right;'
                  },
                  {
                      field: "Names",
                      name: "Name",
                      width: "40%", styles: 'text-align: right;'
                  }

            ]
      ]
}];

</script>

<title>Dojo Data</title>
</head>
<body class=nihilo>
<div style="width: 400px; height: 300px;">
  <div data-dojo-type="dojo.data.ItemFileReadStore" data-dojo-id="countryStoreForGrid" data-dojo-props="data:storeData"></div>
  <div id="grid"
    data-dojo-type="dojox.grid.DataGrid"
    data-dojo-props="store:countryStoreForGrid,
    structure:'gridStructure',
    queryOptions:{deep:true},
    query:{},
    rowsPerPage:40">
  </div>
</div>

</body>
</html>

我在做什么是在页面加载时我正在创建我的 PopulateTextbox 类的对象并调用返回 JSON 字符串的方法。我在ITEMS下传递那个字符串:作为我的网格数据。这是正确的做法吗?我可以在javascript中调用这样的方法吗?如果没有,请建议我正确的方法。当我运行运行此页面时,出现错误::

Webpage error details

Message: Expected ';'
Line: 25
Char: 17
Code: 0
URI: http://localhost:8080/2_8_2012/DisplayData.jsp

这是我写的同一行

PopulateTextbox obj = new PopulateTextbox();

PopulateTextbox.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 net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.google.gson.Gson;

public class PopulateTextbox {

    Gson gson = new Gson();
    JSONObject obj = new JSONObject();
    JSONArray arr = new JSONArray();
    String temp1;

    String temp;
    List <String>rowValues = new ArrayList<String>();
    List <Integer>rowValues1 = new ArrayList<Integer>();
    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,ID FROM EmployeeSearch";
        rs = st.executeQuery(sql);

        while(rs.next()){
            obj.put("ID", rs.getInt("ID"));
            obj.put("Names",rs.getString("Emp_Name"));
            arr.add(obj);
            //obj.add("Name", rs.getString("Emp_Name"));
            //rowValues1.add(rs.getInt("ID"));
            //rowValues.add(rs.getString("Emp_Name"));
            //obj.accumulate("Names",rs.getString("Emp_Name"));
        }
        //obj.accumulate("ID",rowValues1);

        //obj.accumulate("Names",rowValues);
        temp1 = arr.toString();
        System.out.println(temp1);
        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 temp1;

    }
    public static void main(String args[])
    {
        PopulateTextbox obj = new PopulateTextbox();
        String temp1= obj.method();


    }
}

请帮忙。提前致谢..

4

1 回答 1

0

您的 PopulateTextBox 是一个 java 类,但您似乎在 javascript 代码中引用它。

您可以使用 JSP scriptlet 在 JSP 中执行 Java 代码

首先,在 JSP 声明中声明字符串:

<%!
  String javaStr=null;
%>

然后,调用填充字符串的 java 函数:

<%

PopulateTextbox obj = new PopulateTextbox();
String javaStr = obj.method();

%>

然后,按如下方式修改您的 javascript:

 <script type="text/javascript">
    var temp1;
$(document).ready(function() {

temp1 =<%= javaStr %>
});

删除“out.println(temp1);” 来自您的 javascript 代码的行

您可以执行 console.log(temp1) 以在 firebug 控制台中查看数据,或执行 alert(temp1) 以确保数据 JSON 在 temp1 javascript 变量中

于 2012-03-05T21:46:39.340 回答