문제

I am trying to send a new user name to add to one of the multiple tables on my jsp. Each table is defined in a jspf file and included in the jsp. I am trying to use jQuery.ajax() to send the name to my Spring Controller (which then does all the middle and back tier work). When the Spring Controller returns I want the one table to update automatically. I don't want to refresh the entire page.

How do I do this? What do I return from my Spring Controller? I bascially need to refresh the one table (which is in a single jspf file).

Here is an example of the ajax call I am using:

$.ajax('/path/addUser.html', {
  type: "POST",
  data: { "userName" : "bob" },
  success: function(data) {
     // do what if anything?
  },
  error: function(jqXHR, textStatus, errorThrown) {
    Ext.MessageBox.alert('Error', errorThrown);
  }
});
도움이 되었습니까?

해결책

Return username and table name from controller (If you have multiple tables). Then using Jquery just add user name to corresponding table.
In controller put the values in JSON object, you can use Json-simple to encode/decode values in json object.Keep @Responsebody as return type of controller to avoid page/view refresh. In controller

JSONObject json = new JSONObject();
json.put("userName", user_name);
json.put("tableName",table_name);
return json;

In JS decode json values and use jquery like You can use

$(tableName + "tr:last").after(userName);   
  $.ajax('/path/addUser.html', {
  type: "POST",
  data: { "userName" : "bob" },
  success: function(data) {
      $(data.tableName + "tr:last").after(data.userName);   
  },

});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top