2015-04-04 76 views
1

我需要访问自定义编辑器中的一些模型信息。我试图使用ModelMap作为initBinder参数,但是我在运行时获得拒绝错误。 有什么想法?spring mvc4 initBinder模型

public void initBinder(WebDataBinder binder, WebRequest request) { 
    binder.registerCustomEditor(MyData.class, new MyCustomEditor(model)); 
} 

TIA Ice72

回答

0

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