0

How can I use Spring MVC 3.2.3 with bootstrap selectpicker properly?

I create selectpicker in JSP:

<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"/>

and activate it in js:

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

Here is my form handler method header in controller class:

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

I tryed to bind selectpicker selected values to List<String> and List<Integer> in model class (OrganizationDto):

private List<String> affOrgIds;

When I select a few values in selectpicker and submit a form I get an exception:

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

And in chrome developer tools in JSON Request Payload there is only one value:

    affOrgIds:"48"

(Also there is some strange field in json: _affOrgIds:"1". What's that?)

I create this json in JS this way:

var dataObj = $form.serializeJSON();

and then create PUT request by $.ajax() method. When I debug it there is affOrgIds:"48" in dataObj

So how to bind Spring MVC with bootstrap selectpicker to submit multiple selected values?

UPD: I suspect serializeJSON() work not correctly in this situation and I have to create JSON in other way.

RESOLVED: added dataObj.affOrgIds = $form.find('#affOrgId').val();

kostepanych
  • 2,229
  • 9
  • 32
  • 47

1 Answers1

0

$form.serializeJSON() works not correct for bootstrap-selectpicker. To get selectpicker values I use $form.find('#affOrgId').val();

kostepanych
  • 2,229
  • 9
  • 32
  • 47