【问题标题】:Spring bind AJAX form data from key value JSONSpring从键值JSON绑定AJAX表单数据
【发布时间】:2018-05-07 22:06:06
【问题描述】:

我正在尝试绑定与 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。

【问题讨论】:

标签: javascript ajax spring spring-3


【解决方案1】:

问题似乎与 AJAX 请求有关。对每个方法参数使用键值对对数据绑定不起作用。相反,如果我将序列化数据连接到单个字符串中,Spring 将能够正确地将其映射到每个对象。

$.ajax({
method: "GET",
url: contextPath,
data: serialized + "&otherStuff=" + otherStuff,
contentType:"application/json; charset=utf-8",
cache: false,
success: function(data) {});

我不确定是否有更简洁的方法可以避免将对象聚集成单个字符串,但这是功能性的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多