@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));
}