Spring 从键值 JSON 绑定 AJAX 表单数据
Spring bind AJAX form data from key value JSON
我正在尝试绑定在 AJAX 调用中与其他数据一起序列化的表单数据。我似乎无法让 Spring 接收它。它只是实例化一个空表单。
JavaScript:
$.ajax({
method: "GET",
url: contextPath,
data: {"form": serializedForm, "otherStuff": otherStuff},
contentType:"application/json; charset=utf-8",
cache: false,
success: function(data) {});
控制器:
@ResponseBody
@RequestMapping(value="/blah", method=RequestMethod.GET)
public String blah(@ModelAttribute("form") Form form, @RequestParam(value="otherStuff[]", required=false) String[] otherStuff, HttpServletResponse response){
//stuff
}
有没有办法让 Spring 在方法参数中映射我的表单?
似乎我可以从 HttpServletRequest 中单独获取参数,但理想情况下我希望我的表单通过 InitBinder。
问题似乎与 AJAX 请求有关。对每个方法参数使用键值对不适用于数据绑定。相反,如果我将序列化数据连接成一个字符串 Spring 将能够正确地将它映射到每个对象。
$.ajax({
method: "GET",
url: contextPath,
data: serialized + "&otherStuff=" + otherStuff,
contentType:"application/json; charset=utf-8",
cache: false,
success: function(data) {});
我不确定是否有更简洁的方法来避免将您的对象聚集成一个字符串,但这是实用的。
我正在尝试绑定在 AJAX 调用中与其他数据一起序列化的表单数据。我似乎无法让 Spring 接收它。它只是实例化一个空表单。
JavaScript:
$.ajax({
method: "GET",
url: contextPath,
data: {"form": serializedForm, "otherStuff": otherStuff},
contentType:"application/json; charset=utf-8",
cache: false,
success: function(data) {});
控制器:
@ResponseBody
@RequestMapping(value="/blah", method=RequestMethod.GET)
public String blah(@ModelAttribute("form") Form form, @RequestParam(value="otherStuff[]", required=false) String[] otherStuff, HttpServletResponse response){
//stuff
}
有没有办法让 Spring 在方法参数中映射我的表单?
似乎我可以从 HttpServletRequest 中单独获取参数,但理想情况下我希望我的表单通过 InitBinder。
问题似乎与 AJAX 请求有关。对每个方法参数使用键值对不适用于数据绑定。相反,如果我将序列化数据连接成一个字符串 Spring 将能够正确地将它映射到每个对象。
$.ajax({
method: "GET",
url: contextPath,
data: serialized + "&otherStuff=" + otherStuff,
contentType:"application/json; charset=utf-8",
cache: false,
success: function(data) {});
我不确定是否有更简洁的方法来避免将您的对象聚集成一个字符串,但这是实用的。