【问题标题】:Spring MVC + bootstrap-select multipleSpring MVC + bootstrap-select 多个
【发布时间】:2017-06-26 07:40:01
【问题描述】:

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

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

并在js中激活:

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

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

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

我尝试将 selectpicker 选择的值绑定到模型类 (OrganizationDto) 中的 List&lt;String&gt;List&lt;Integer&gt;

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: 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

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

    affOrgIds:"48"

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

我是这样在 JS 中创建这个 json 的:

var dataObj = $form.serializeJSON();

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

那么如何绑定 Spring MVC 和 bootstrap selectpicker 来提交多个选中的值呢?

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

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

【问题讨论】:

  • 我不明白这个JSON 来自哪里。你用@RestController吗?
  • 抱歉忘记写了...更新了...
  • 您应该回答您自己的问题,或者如果已解决,请关闭此问题。

标签: java spring spring-mvc bootstrap-select bootstrap-selectpicker


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 2012-07-06
    • 1970-01-01
    相关资源
    最近更新 更多