2015-04-04 34 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)); 
}