0

如何正确使用Spring MVC 3.2.3和bootstrap selectpicker?Spring MVC + bootstrap-select multiple

我在JSP中创建selectpicker:

<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(); 

这里是控制器类我的表单处理方法标题:

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

我tryed将selectpicker选定值绑定到List<String>List<Integer> mod埃尔班(OrganizationDto):

private List<String> affOrgIds; 

当我选择在selectpicker几个值,并提交形式我得到一个异常:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token 
at [Source: [email protected]; 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 

,并在JSON请求负载Chrome开发者工具只有一个值:

affOrgIds:"48" 

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

我在JS创建这个JSON这样:

var dataObj = $form.serializeJSON(); 

,然后创建由$.ajax()方法PUT请求。 当我调试它affOrgIds:"48"dataObj

那么如何绑定Spring MVC与引导selectpicker提交多个选定的值?

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

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

+0

我不明白这个'JSON'来自哪里。你使用'@ RestController'? – sedooe

+0

抱歉,忘了编写它...更新中...... – kostepanych

+0

您应该回答自己的问题或解决此问题。 –

回答

0

$ form.serializeJSON()的作品不正确的引导,selectpicker。 要获得selectpicker值,我使用$ form.find('#affOrgId')。val();