我目前使用的是Spring MVC,我正在嘗試使用ajax做一些事情。基本上我現在要做的就是在網頁上動態顯示來自控制器的結果。在Spring MVC中使用ajax
I.E.用戶按下一個按鈕後,它會轉到「whatever.do」控制器,並獲取一個列表並顯示該列表,而無需重新加載該頁面。
無論如何有誰知道任何好的教程或示例項目?
我目前使用的是Spring MVC,我正在嘗試使用ajax做一些事情。基本上我現在要做的就是在網頁上動態顯示來自控制器的結果。在Spring MVC中使用ajax
I.E.用戶按下一個按鈕後,它會轉到「whatever.do」控制器,並獲取一個列表並顯示該列表,而無需重新加載該頁面。
無論如何有誰知道任何好的教程或示例項目?
這是非常簡單的,我甚至不認爲需要進行個別輔導(除了從一個普通的彈簧MVC一個)。
@RequestMapping("/foo")
方法返回一個List<Foo>
<mvc:annotation-driven />
在dispatcher-servlet.xml
激活的處理器映射和轉換器$.getJSON("/foo", function(data) {..});
(jQuery的)Foo
對象的JSON編碼列表Spring wi會檢測到瀏覽器請求json響應,並使用Jackson轉換您的對象。
控制器必須具有以下格式當你與春天同阿賈克斯一起使用:
@RequestMapping(value = "/show.ajax", method = RequestMethod.POST)
public @ResponseBody List<your object type> your_method_name() {
//code logic
return list_of_your_object;
}
,可以在以下格式JSP頁面上的Java腳本代碼:
<script>
function your_javascript_fun_name() {
$.ajax({
type : 'POST',
url : 'show.ajax',//this is url mapping for controller
success : function(response) {
alert(response);
//this response is list of object commming from server
},
error : function() {
alert("opps error occured");
}
});
}
</script>
進口jQuery庫在jsp頁面
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
請在jquery響應中添加迭代代碼 –
okey明白!謝謝 –
var id = $("#id").val();
var resourceURL = $("#contextpath").val()+"/customer/retrivebyid/"+id;
$.ajax({
url : resourceURL,
type : 'GET',
dataType : 'json',
async : false,
success: function(data) {
var successflag = data.response.successflag;
var errors = data.response.errors;
var results = data.response.result;
if(successflag == "true"){
$.each(results, function (i, result) {
$("#id").val((result.id == undefined || result.id == null || result.id.length <= 0) ? "-" : result.id);
$("#name").val((result.customername == undefined || result.customername == null || result.customername.length <= 0) ? "-" : result.customername);
});
}else{
$("#errorMsgContent").html(errors);
$.fancybox.open('#errorMsg');
}
},
error: function (xhr, ajaxOptions, thrownError) {
$("#errorMsgContent").html(thrownError);
$.fancybox.open('#errorMsg');
}
});
請嘗試包含解決方案的一些解釋。僅有代碼的答案不如解釋發生了什麼的那些有用。 – Antimony
@RequestMapping(value = "/retrivebyid/{id}", method = RequestMethod.GET)
public @ResponseBody String retriveUser(@PathVariable long id, Model model,HttpServletRequest request) {
String jsonresp = null;
try {
List<CustomerDO> customerList = new CustomerService().retriveById(id);
jsonresp = CustomerUtil.getCustomerList(customerList).toString();
} catch (Exception e) {}
if (jsonresp != null) {
return jsonresp.toString();
} else {
return null;
}
}
public static JSONObject getCustomerList(List<CustomerDO> empList)throws AppException {
JSONObject responseJSON = new JSONObject();
JSONObject resultJSON = new JSONObject();
try {
resultJSON.put(CommonConstants.SUCCESS_FLAG, CommonConstants.TRUE);
resultJSON.put(CommonConstants.ERRORS, "");
JSONArray resultJSONArray = new JSONArray();
for (CustomerDO customer : empList) {
resultJSONArray.put(getCustomerDetailObject(customer));
}
resultJSON.put(CommonConstants.RESULTS, resultJSONArray);
responseJSON.put(CommonConstants.RESPONSE, resultJSON);
} catch (JSONException e) {
e.printStackTrace();
}
return responseJSON;
}
private static JSONObject getCustomerDetailObject(CustomerDO customer)throws JSONException, AppException {
JSONObject result = new JSONObject();
result.put(CommonConstants.ID, String.valueOf(customer.getId()));
result.put(CommonConstants.CUSTOMER_NAME, String.valueOf(customer.getName()));
return result;
}
雖然這可能在理論上回答這個問題,但[這將是更可取的](http://meta.stackexchange.com/q/8259)在這裏包含答案的基本部分,並提供供參考的鏈接。 –