java - ajax 将序列化的表单和模型列表发布到控制器方法
问题描述
我需要将视图表单、字符串列表和对象列表发布到控制器方法。
我的控制器方法看起来像这样
@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]:指定的类是一个接口
解决方案
添加@RequestBody
说明参数绑定到 HTTP 请求正文的注释。
推荐阅读
- chatbot - AIML 聊天机器人可以支持来自用户的无限查询吗?
- html - 输入字段在 IE 11 中潜入带有标签的 div
- laravel - "'SELECT s.id AS 'Salarie.id', s.nom AS 'Salarie.nom', COALESCE(p' on line 1'附近的语法错误
- php - 排序查询 ASC 给出错误的顺序
- ansible - 使用变量连接时未达到 Ansible 默认值
- python - 无法拟合超级简单数据。为什么?
- sql - SQL 仅为带有附加条件的最新 ID 创建列
- android - 无法通过 Gradle 启用 Android Lint 规则
- python - 使用 Python 从 XML 中获取值
- node.js - 等待 then() 完成