如何正確使用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();
我不明白這個'JSON'來自哪裏。你使用'@ RestController'? – sedooe
抱歉,忘了編寫它...更新中...... – kostepanych
您應該回答自己的問題或解決此問題。 –