2012-03-05 19 views
0

我有一個類,其中我有一個方法名方法()以JSON字符串的形式返回一個字符串。輸出是::添加Json數據作爲dojogrid的源代碼

[{"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下傳遞該字符串:作爲我的Grid數據。這是幹什麼的正確方法?我可以在JavaScript中調用這樣的方法嗎?如果沒有,請給我正確的方法。當我運行運行此頁我得到一個錯誤::

Webpage error details 

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

它我在哪裏寫

PopulateTextbox的obj =新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(); 


    } 
} 

請幫助。在此先感謝..

回答

0

您的PopulateTextBox是一個java類,但你似乎是在JavaScript代碼中引用它。

您可以使用JSP腳本在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中)以查看螢火控制檯,或警報(temp1中)的數據,以保證數據JSON是在temp1中JavaScript變量

相關問題