0
我有動態變化的參數列表,我需要從jsp頁面傳遞給spring mvc控制器,我該如何實現這一點?請提供一個建議或一個僞代碼,以便我可以繼續我的發展。提交從jsp到控制器的對象列表
我有動態變化的參數列表,我需要從jsp頁面傳遞給spring mvc控制器,我該如何實現這一點?請提供一個建議或一個僞代碼,以便我可以繼續我的發展。提交從jsp到控制器的對象列表
您可以通過POST或GET將.jsp中的ajax用於控制器。
.JSP:
<%
Gson gson = new Gson();
%>
<script>
var json = "<% out.println(gson.toJson(myList).toString()); %>";
function sendData() {
"use strict";
var xhr;
xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
console.log("data send to my controller ...");
}
};
xhr.open("GET", "http://localhost:8080/url_of_my_controller?value=" + json, false);
xhr.send(null);
}
</script>
controller.java:
@RequestMapping(value = "/url_of_my_controller", method = RequestMethod.GET)
public @ResponseBody String controller(HttpServletRequest request, HttpServletResponse response) {
if (request.getParameter("value") != null) {
Gson gson = new Gson();
List<foo> list = gson.fromJson(request.getParameter("value"), List<foo>);
System.out.println(request.getParameter("value"));
return ("ok");
}
return ("ko");
}
你有沒有考慮這一點:
<form id="myForm" action="blammo">
<input type="hidden" name="hoot"/> <!-- dynamically add hidden elements to -->
<!-- myForm then submit it -->
</form>