我需要在自定义编辑器中访问某些模型信息。我试图将ModelMap用作initBinder参数,但我在运行时获得了拒绝错误。 有什么想法吗?
public void initBinder(WebDataBinder binder, WebRequest request) {
binder.registerCustomEditor(MyData.class, new MyCustomEditor(model));
}
TIA Ice72
答案 0 :(得分:0)
@InitBinder只需注册编辑器和验证器,即映射HTML< form>的ModelMap。将在initBinder之后,在@RequestMapping方法之前创建。
您可以在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));
}