您可以将自定义编辑器注册到控制器类的initBinder中:
@Controllerpublic class CoinViewController { @Autowired private CountryEditor countryEditor; @InitBinder protected void initBinder(final WebDataBinder binder, final Locale locale) { binder.registerCustomEditor(Country.class, countryEditor); } ......}(
locale在这种情况下不需要参数,但是如果您需要使用语言环境进行转换,例如在使用日期时,该参数将很有用)
您可以定义
CountryEditor以下内容:
@Componentpublic class CountryEditor extends PropertyEditorSupport { @Autowired private CountryService countryService; @Override public void setAsText(final String text) throws IllegalArgumentException { try{ final Country country = countryService.findById(Long.parseLong(text)); setValue(cliente); }catch(Exception e){ setValue(country); // or handle your exception } }}我让spring用
@Component注解处理编辑器的注入。因此,如果您喜欢这种方式,请记住为该类启用程序包扫描!
希望有帮助!



