【问题标题】:Spring mvc binding exception on submitting select box提交选择框时的Spring mvc绑定异常
【发布时间】:2012-10-15 23:38:15
【问题描述】:

我有一个包含国家、州和城市下拉框的 JSP。城市默认填充,然后州和城市将基于使用 ajax 选择的国家和州。

 <form:select path="ContactInfoVO[0].countryList" multiple="single" id="country">       
<form:option value="-1" label="-- Select Country--"></form:option>                      
<c:forEach var="country" items="${ManagerVO.ContactInfoVO[0].countryList}" varStatus="item">
<form:option value="${country.countryId}" label="${country.countryName}"/></c:forEach> </form:select>   

<form:select path="ContactInfoVO[0].stateList" multiple="single" id="state" class="small">      
<form:option value="-1" label="-- Select State--"></form:option>                        
<c:forEach var="state" items="${ManagerVO.ContactInfoVO[0].stateList}" varStatus="item">
<form:option value="${state.stateId}" label="${state.stateName}"/></c:forEach></form:select>    

<form:select path="ContactInfoVO[0].cityList" multiple="single" id="city" class="validate[required] small">     
<form:option value="-1" label="-- Select City--"></form:option>                     
<c:forEach var="city" items="${ManagerVO.ContactInfoVO[0].cityList}" varStatus="item">
<form:option value="${city.cityId}" label="${city.cityName}"/></c:forEach></form:select>

当我提交此表单时,我收到错误 -

 org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 3 errors
    Field error in object 'ManagerVO' on field 'ContactInfoVO[0].cityList': rejected value [80930]; codes [typeMismatch.ManagerVO.ContactInfoVO[0].cityList,typeMismatch.ManagerVO.ContactInfoVO.cityList,typeMismatch.ContactInfoVO[0].cityList,typeMismatch.ContactInfoVO.cityList,typeMismatch.cityList,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [ManagerVO.ContactInfoVO[0].cityList,ContactInfoVO[0].cityList]; arguments []; default message [ContactInfoVO[0].cityList]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'ContactInfoVO[0].cityList'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.admin.command.CityVO] for property 'cityList[0]': no matching editors or conversion strategy found]
    Field error in object 'ManagerVO' on field 'ContactInfoVO[0].countryList': rejected value [31]; codes [typeMismatch.ManagerVO.ContactInfoVO[0].countryList,typeMismatch.ManagerVO.ContactInfoVO.countryList,typeMismatch.ContactInfoVO[0].countryList,typeMismatch.ContactInfoVO.countryList,typeMismatch.countryList,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [ManagerVO.ContactInfoVO[0].countryList,ContactInfoVO[0].countryList]; arguments []; default message [ContactInfoVO[0].countryList]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'ContactInfoVO[0].countryList'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.admin.command.CountryVO] for property 'countryList[0]': no matching editors or conversion strategy found]
    Field error in object 'ManagerVO' on field 'ContactInfoVO[0].stateList': rejected value [601]; codes [typeMismatch.ManagerVO.ContactInfoVO[0].stateList,typeMismatch.ManagerVO.ContactInfoVO.stateList,typeMismatch.ContactInfoVO[0].stateList,typeMismatch.ContactInfoVO.stateList,typeMismatch.stateList,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [ManagerVO.ContactInfoVO[0].stateList,ContactInfoVO[0].stateList]; arguments []; default message [ContactInfoVO[0].stateList]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'ContactInfoVO[0].stateList'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.admin.command.StateVO] for property 'stateList[0]': no matching editors or conversion strategy found]

谁能告诉我怎么解决?

【问题讨论】:

    标签: spring spring-mvc


    【解决方案1】:

    输出中存在错误:您正在尝试将类型为 List 的域类字段 ContactInfoVO[0].cityList 设置为 String 值。

    没有看到您的域类的代码等,很难知道您的页面打算做什么。如果您尝试使用用户确定的单个值填充数据库,那么通常使用来自不同表(或类/枚举)的值填充选择下拉列表,然后将用户选择的单个值设置为自己的类/表。看起来你正试图用同一个班级做这两件事。

    【讨论】:

    • 我只是想根据州和国家来填充城市,在提交表单时我将只使用 cityId。所有三个字段(国家、州、城市)都有自己的主表,从中填充选择框。现在,当我点击提交时,spring 甚至在到达控制器之前就引发了这个错误,这意味着模型属性的绑定失败。你能给我一些建议,这样我就可以在我的控制器上只获得 cityId 吗?
    • 我认为您需要更改 &lt;form:select path="ContactInfoVO[0].cityList"&gt; 以绑定到不同的路径。一个采用String 值,而不是List。 ContactInfoVO[0].cityList 需要一个列表,而您正尝试将其设置为字符串。因此,您似乎正在尝试将 cityId 设置回您的主列表,而不是您希望保存数据的表。
    • 谢谢,不知道我之前怎么没注意到这个:(但这解决了问题
    【解决方案2】:

    我看到您正在尝试将 countryId 设置(例如)为 ContactInfoVO[0].countryList。所以你正在将一个整数设置为一个列表。

    我认为问题出在这里:

     <form:select path="ContactInfoVO[0].countryList" multiple="single" id="country"> 
    

    我认为countryList 没有任何意义。它应该类似于ContactInfo[0].countryId,它与您选择的内容相匹配,在本例中为"${country.countryId}"

    如果您发布的 ContractInfoVO 类和您的标签会更清楚。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-27
      • 2016-05-27
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      • 2016-03-11
      • 1970-01-01
      相关资源
      最近更新 更多