【发布时间】:2015-11-30 13:02:47
【问题描述】:
我有两个链接的对象。 Address 有一个 Country 和一个由 countryId 链接的 ManyToOne 关系。我正在尝试使用 JSP 添加网页以添加新的Address。在此页面上,您可以从下拉列表中选择 Country。
我尝试了多种不同的方法来做到这一点,但我似乎无法将下拉列表中的 Country 对象链接到 Address 中的 Country 对象,我不断收到以下错误
Field error in object 'address' on field 'country': rejected value [com.company.project.domain.Country@670b11d7]; codes [typeMismatch.address.country,typeMismatch.country,typeMismatch.com.company.project.domain.Country,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [address.country,country]; arguments []; default message [country]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'com.company.project.domain.Country' for property 'country'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.company.project.domain.Country] for property 'country': no matching editors or conversion strategy found
这是我尝试过的许多不同选项中的两个,但我一直收到相同的错误
试试 1 JAVA
List<Country> countries = countryService.getAllCountrys();
LinkedHashMap<Country, String> countryMap = new LinkedHashMap<Country, String>();
for(Country country : countries){
countryMap.put(country, country.getCountryName());
}
model.addAttribute("countries", countryMap);
试试 1 JSP
<tr>
<td>Country:</td>
<td><form:select path="country">
<form:options items="${countries}" />
</form:select>
</td>
</tr>
试试 2 JAVA
List<Country> countries= countryService.getAllCountry();
model.addAttribute("countries", countries);
试试 2 JSP
<tr>
<td>Country:</td>
<td><form:select path="country" id="country">
<form:options items="${countries}" itemValue="country" itemLabel="countryName" />
</form:select>
</tr>
我可以正确填充下拉列表,但我尝试保存它似乎是在尝试保存字符串值而不是对象值。我正在使用 POST 请求来保存页面
有人知道怎么做吗?
【问题讨论】:
标签: javascript java jquery spring jsp