簡體   English   中英

如何使用Extjs進行Ajax調用以從jsp中的數據庫中獲取數據?

[英]how can I do ajax call to fetch data from database in jsp using Extjs?

我正在使用Extjs框架。 首先,我創建一個servlet從數據庫中獲取數據。 從數據庫中獲取數據后,我將存儲在列表中。 然后我將列表轉換為json數組,現在我想使用Extjs ajax調用將這些數據放入jsp頁面。 我怎樣才能做到這一點? 我的servlet是

String str = "select * from employee";
                    rs = stmt.executeQuery(str);
                    List<Employee> list1 = new ArrayList<Employee>();
                    while(rs.next){
                        emp = new Employee();
                        emp.setEmpId(rs.getInt(1));
                        emp.setFirstName(rs.getString(2));
                        list1.add(emp);
                    }
                    Gson gson = new Gson();
                    JsonElement element = gson.toJsonTree(list1, new TypeToken<List<Employee>>() {
                    }.getType());
                    JsonArray jsonArray = element.getAsJsonArray();
                    response.setContentType("application/json");
                    response.getWriter().print(jsonArray);

現在,我通過Extjs單擊按鈕后就在jsp文件中調用此servlet。 我怎么做ajax電話? 我正在用這個..

    Ext.onReady(function () {
    Ext.create('Ext.Button', {
        text: 'click',
        handler: function () {  
            Ext.Ajax.request({
           url: 'TestEmployee',
       success: function(result, request) {
      /////what shold I do here so that I get data here and display in jsp???
   },
   failure: function(response, opts) {
      Ext.MessageBox.alert('Failed', result.responseText);
   }
});
        },   
        renderTo: Ext.getBody()

    });
    });

嘗試

var array = Ext.decode(result.responseText);

您正在使用哪個版本的ExtJ?

我建議您使用Ext 4,以便可以使用MVC模式。

我將嘗試在網格中呈現員工列表,因此我將通過store.load()進行ajax請求,其中商店將被綁定到網格。

這是Extjs 4的基本教程

http://docs.sencha.com/extjs/4.2.2/#!/guide/getting_started

http://docs.sencha.com/extjs/4.2.2/#/guide/application_architecture

最好的祝福。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM