@InitBinder刚刚登记的编辑器和验证器,其映射HTML <形式>将initBinder之后创建,则@RequestMapping方法之前ModelMap。
您可以使用CustomEditor中的服务,并格式化将要创建的ModelMap的字段。
public class MyCustomEditor extends PropertyEditorSupport {
private YourService service;
public MyCustomEditor(YourService service){
this.service = service;
}
@Override
public void setAsText(String text){
// TODO service can work here
setValue(text);
}
@Override
public String getAsText(){
// TODO service can work here
return (String) getValue();
}
}
@Resource
YourService yourService;
@InitBinder
protected void initBinder(WebDataBinder binder){
binder.registerCustomEditor(MyData.class, "fieldInMyData", new MyCustomEditor(yourService));
}