2014-02-11 33 views

回答

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"); 
} 
0

你有沒有考慮這一點:

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