spring mvc form:选择标签
我有一个Model,它包含一个Country(List)列表和一个包含Country对象的用户对象。 我认为用户可以选择他的国家。
这是我的jsp页面的片段:
这是我的帐户模型:
public class Account { private User user; private List countries; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public List getCountries() { return countries; } public void setCountries(List countries) { this.countries = countries; } }
当jsp加载(GET)表单时:select显示当前用户所在国家/地区的选定项目。 问题是,当我发布表单时,我得到这个例外:
Field error in object 'account' on field 'user.country': rejected value [90]; codes [typeMismatch.account.user.country,typeMismatch.user.country,typeMismatch.country,typeMismatch.org.MyCompany.entities.Country,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [account.user.country,user.country]; arguments []; default message [user.country]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.MyCompany.entities.Country' for property 'user.country'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.MyCompany.entities.Country] for property 'country': no matching editors or conversion strategy found]
知道如何克服这个问题吗?
你需要以某种方式告诉Spring将String
转换为Country
。 这是一个例子:
@Component public class CountryEditor extends PropertyEditorSupport { private @Autowired CountryService countryService; // Converts a String to a Country (when submitting form) @Override public void setAsText(String text) { Country c = this.countryService.findById(Long.valueOf(text)); this.setValue(c); } }
和
... public class MyController { private @Autowired CountryEditor countryEditor; @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Country.class, this.countryEditor); } ... }
- 使用REST优化大批量插入Neo4j
- 什么是与查询不服务相关的最佳和最全面的SOAP库
- 再次使用java中的字符串比较
- Netbeans的历史显示了100个变化,但我只做了几个。 我该如何避免这种情况?
- 无法设置gradle:’无法找到主类:org.gradle.launcher.GradleMain。’
- 如何使用VIM / GVIM调试Java应用程序?
- 如何在JTextArea中显示日语字符
- 从S3下载文件> 3Gb失败,出现“SocketTimeoutException:Read timed out”
- Collection – Iterator.remove()vs Collection.remove()