2014-05-09 66 views
0

我想要獲取所有錶行並將它們添加到hashmap數組中,然後將它們轉換爲json對象以使用ajax和jquery發送到jsp頁面。我在顯示jsp頁面中的內容時遇到問題。 我已經設法編寫下面的代碼,需要幫助來糾正/改進它,我很困惑如何在jsp頁面中顯示它。我是jQuery和json的初學者。將HahMap數組轉換爲json對象並將其顯示在jsp頁面

try { 
     String res = request.getParameter("hello"); 
     String user = ""; 
     String pass = ""; 
     String port = ""; 
     String dbname = ""; 
     String host = ""; 
     String driver = "oracle.dbc.driver.OracleDriver"; 
     Connection con; 
     PreparedStatement ps; 
     ResultSet rs; 
     String json = null; 
     String url = "jdbc:oracle:thin:@"+host+":"+port+":"+dbname; 

     Map<String,String> map = new HashMap<String,String>(); 
     List<Map<String,String>> list = new ArrayList<Map<String,String>>(); 

     try{ 
      Class.forName(driver).newInstance(); 
      con = DriverManager.getConnection(url,user,pass); 
      ps = con.prepareStatement("select * from employee"); 

      rs = ps.executeQuery(); 
      while(rs.next()){ 
       map.put("name", rs.getString(1)); 
       map.put("id", rs.getString(2)); 
       map.put("salary",rs.getString(3)); 
       list.add(map); 
      }     
      json = new Gson().toJson(list); 
      response.setContentType("application/json"); 
      response.setCharacterEncoding("UTF-8"); 
      response.getWriter().write(json); 
     }catch(Exception e){    
     } 


    } finally {    
     out.close(); 
    } 

的index.jsp

<script src="http://code.jquery.com/jquery-latest.js"></script> 
    <script> 
     $(document).ready(function(){ 
      $("#push").click(function(){ 
       var value = $("#t").val(); 
       $.post('jsonServlet',{hello:value},function(data){ 

      //  ?? 
       // ?? 
       // ?? 

       }); 
      });    
     }); 
    </script> 
</head> 
<body style="position: absolute;min-height: 700px;min-width: 1300px"> 
    <form> 
     <input type="button" value="PushMe" id="push"> 
     <input type="text" id="t" value="my data"> 
     <div style="position: absolute;height: 50%;width: 60%;" id="myDiv"> 

     </div> 
    </form>   
</body> 

需要幫助的JSP頁面來顯示在DIV哈希表的內容...... 後來我將改變DIV使用文本框,存儲各個列值,以便該用戶可以修改表格的內容並再次更新表格行。

需要幫助

回答

2

使用地圖看起來EW,使一個類來代替:

class Person{ 
    public final String name; 
    public final String id; 
    public final String salary; 
    public Person(String name, String id, String salary){ 
     this.name=name; 
     this.id=id; 
     this.salary=salary; 
    } 
} 

並不需要成爲一個公共類,可以把它放在你的servlet的java文件。 Gson知道如何將類序列化爲json。

讀自數據庫:

while(rs.next()){ 
    list.add(new Person(rs.getString(1),rs.getString(2),rs.getString(3))); 
} 

在你的JavaScript是這樣的:

$.post(
    'jsonServlet', // <- may screw up path here 
    { hello: value }, 
    function(data){ 
     var table = '<table>'; 
     for(var i = data.length, l = data.length; i>0; i--){ 
      // one of over 9000 ways to make js loops work faster 
      var person = data[l-i]; // <- this one also does not screw up order 
      var row = '<tr>'; 
      row +='<td>'+person.name+'</td>'; 
      row +='<td>'+person.id+'</td>'; 
      row +='<td>'+person.salary+'</td>'; 
      row +='</tr>'; 
      table += row; 
     } 
     table += '</table>'; 
     $('#myDiv').html(table); 
    }, 
    'json' 
); 

您可能希望實際上在你的JSP事先地方擺好桌子,也許在一個隱藏的股利和使用它作爲一個模板。

+0

關於()的設計:從現在開始,JavaScript向下循環比前向循環要快上百倍,在所有瀏覽器中一致。緩存數組的長度幾乎沒有任何影響。 http://jsperf.com/loopy-arrays/3 – enlait

+0

謝謝你,先生,真的很有幫助。 – Kharoud

+0

你好先生我知道我標記爲已解決,但仍然存在問題。我沒有從我的jsp中獲取servlet的值。甚至沒有簡單的字符串。在我的gson()方法中有沒有問題,或者在Ajax調用中有沒有問題? – Kharoud