2012-03-05 2 views
0

JSON 문자열 형태로 String을 반환하는 메소드 이름 method()가있는 클래스가 있습니다. 결과는 ::dojogrid의 소스로 Json 데이터 추가

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

이제는 Dojo를 사용하여 Datagrid에 채우기를 원하는 데이터 형식으로되어 있습니다. 다음과 같이 :: 난 내 PopulateTextbox 클래스의 객체를 만들고 JSON 문자열을 반환하는 메서드를 호출하고있는 페이지가로드로 내가 뭐하는 거지

<%@ 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> 

이 내 JSP 페이지에 나는 그것을 사용하고 있습니다. 그리고 그 문자열을 ITEMS : Grid에 대한 나의 데이터로 전달합니다. 이게 올바른 일인가요? 자바 스크립트에서이 같은 메서드를 호출 할 수 있습니까? 그렇지 않다면 제게 올바른 길을 제안하십시오. 내가이 페이지를 실행 실행할 때 오류 ::

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는 자바 클래스이지만 자바 스크립트 코드에서 참조하는 것처럼 보입니다. 그런 다음

<%! 
    String javaStr=null; 
%> 

, 문자열 웁니다 자바 함수를 호출 :

당신은

첫번째 JSP에서 자바 코드를 실행 JSP로 선언에서 문자열을 선언하는 JSP 스크립틀릿을 사용할 수 있습니다

<% 

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

%> 

다음과 같이 자바 스크립트를 수정하십시오.

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

temp1 =<%= javaStr %> 
}); 

"out.println (temp1);"을 제거하십시오. 당신의 자바 스크립트 코드에서 라인

당신은 데이터 JSON은 TEMP1에서 자바 스크립트 변수를 확인하기 위해 불을 지르고 콘솔 또는 경고 (TEMP1)의 데이터를 볼 수있는 CONSOLE.LOG (TEMP1을) 할 수

관련 문제