将对象列表从jsp提交到控制器

时间:2014-02-11 15:36:27

标签: javascript spring jsp spring-mvc jsp-tags

我有动态变化的参数列表,我需要从jsp页面传递给spring mvc控制器,我该如何实现?请提供建议或伪代码,以便我可以继续开发。

2 个答案:

答案 0 :(得分:0)

您可以使用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");
}

答案 1 :(得分:0)

你有没有考虑过这个:

<form id="myForm" action="blammo">
  <input type="hidden" name="hoot"/> <!-- dynamically add hidden elements to -->
                                     <!-- myForm then submit it -->
</form>