【问题标题】:ajax post serialized form and model list to controller methodajax 将序列化的表单和模型列表发布到控制器方法
【发布时间】:2019-08-28 16:00:46
【问题描述】:

我需要将视图表单、字符串列表和对象列表发布到控制器方法。

我的控制器方法如下所示

@ResponseBody
@RequestMapping(value = "/get-client-policies", method = RequestMethod.POST)
public ModelAndView getClientPolicies(@ModelAttribute("claimRegistrationForm") ClaimRegistrationForm claimRegistrationForm,
                                      @ModelAttribute("healthCardsList") List<String> healthCardsList,
                                      @ModelAttribute("bankAccountsList") List<CustomerBankAccounts> bankAccountsList,
                                      Map<String, Object> model) {

    return new ModelAndView("claims/reg/_form", model);
}

我的 ajax 调用:

$.ajax({
    type: 'POST',
    url: BASEHREF + 'ajax/claim-registration/get-client-policies,
    data: 'claimRegistrationForm=' + $('#claim_reg_form').serialize()
        + '&healthCardsList=' + $.parseJSON($('#healthCardsList').val())
        + '&bankAccountsList=' + $('#bankAccountsList').val(),
    success: function (response) {},
    error: function (jqXHR, error, errorThrown) {}
});

我的问题是如何发布 bankAccountList 参数。

BankAccountList 是使用字符串化的

@Override
public String toString() {
    return Pojomatic.toString(this);
}

并以表单形式存储在隐藏的输入字段中

<input type="hidden" id="bankAccountsList" th:value="${bank_accounts}" />

$('#bankAccountsList').val()的值为

"[CustomerBankAccount{bankCode: 1, bankAccountNo: 2, bankName: {name1}},
CustomerBankAccount{bankCode: 2, bankAccountNo: 3, bankName: {name2}}]"

在 ajax 发布之后,我得到了

org.springframework.beans.BeanInstantiationException: 失败 实例化[java.util.List]:指定的类是一个接口

【问题讨论】:

    标签: java ajax spring-mvc post


    【解决方案1】:

    添加注解@RequestBody,表示参数绑定到HTTP请求体。

    【讨论】:

    • 我试过了。但是我们的应用程序不支持 RequestBody?一些配置可能是有序的。我收到了不允许的例外情况,但我未能找到有关如何使其正常工作的解决方案。
    猜你喜欢
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2016-06-21
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多