Spring MVC + bootstrap-select 多个

Spring MVC + bootstrap-select multiple

如何正确使用 Spring MVC 3.2.3 和 bootstrap selectpicker?

我在 JSP 中创建了 select 选择器:

<form:form role="form" id="orgForm" commandName="orgDto" >
...
<form:select id="affOrgId" class="form-control selectpicker" path="affOrgIds" itemValue="id" multiple="true" items="${organizations}" itemLabel="label"/>

并在js中激活它:

$('.selectpicker').selectpicker();

这是我在控制器 header 中的表单处理程序方法 class:

  @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public @ResponseBody
OrganizationDto updateOrganization(@PathVariable Long id,@Valid @RequestBody OrganizationDto org) {

我尝试将 selectpicker selected 值绑定到模型 class (OrganizationDto):

中的 List<String>List<Integer>
private List<String> affOrgIds;

当我在 select 选择器中 select 一些值并提交表单时,我得到一个异常:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token
at [Source: org.apache.catalina.connector.CoyoteInputStream@5c82a3e5; line: 1, column: 205] (through reference chain: com.org.OrganizationDto["affOrgIds"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token

而在 chrome 开发者工具中 JSON Request Payload 只有一个值:

    affOrgIds:"48"

(json 中还有一些奇怪的字段:_affOrgIds:"1"。那是什么?)

我在 JS 中这样创建 json:

var dataObj = $form.serializeJSON();

然后通过$.ajax()方法创建PUT请求。 当我调试它时 dataObj

中有 affOrgIds:"48"

那么如何将 Spring MVC 与 bootstrap selectpicker 绑定以提交多个 selected 值?

UPD: 我怀疑 serializeJSON() 在这种情况下无法正常工作,我必须以其他方式创建 JSON。

已解决: 添加了 dataObj.affOrgIds = $form.find('#affOrgId').val();

$form.serializeJSON() 不适用于 bootstrap-selectpicker。 要获取选择器值,我使用 $form.find('#affOrgId').val();