2013-08-29 105 views
0

我在JSP页面上有单选按钮。InitBinder不被调用/调用

<form:form modelAttribute="obj" action="save"> 
    <form:radiobuttons path="person" items="${List}"/> 

<form:button>Save</form:button> 
</form:form> 

列表:的人对象列表 的OBJ:

Class obj{ 
    Person person; 
    getter/setters 
} 

在这JSP,我有当选择特定单选按钮附加的人。

在控制器端

@RequestMapping(value = "/save", method = RequestMethod.POST) 
     public String postProcess(@ModelAttribute("obj") Obj obj,BindingResult error) { 

      //processing 
      return "anotherJsp"; 
     } 


    @InitBinder 
    public void initBinder(WebDataBinder binder, Locale locale, HttpServletRequest request) { 
    binder.registerCustomEditor(Obj.class,"person", new PropertyEditorSupport() { 
     @Override 
     public void setAsText(String text) { 
      //processing for conversion of person object String to Person Object 
      setValue(text); 
     } 
    }); 
} 

我在这里使用initbinder,因为我正从JSP Person对象的字符串...所以我得到的结合异常无法从字符串转换成个人

当将数据绑定到modelAttribute.Hence时,在initbinder中我会将字符串转换为Person对象之前调用InitBinder。

这里主要的问题是我的InitBinder在Not called/Invoked。 请给我解决方案。

谢谢。

回答

2

请检查它是否通过放置断点或调试语句来调用。

您的模型属性被命名为“obj”。但在propertyeditor中,您使用“人”作为物业路径。

请尝试使用“obj”作为propertyPath或删除该参数并使用此签名。 registerCustomEditor(Class requiredType, PropertyEditor propertyEditor)

希望它有帮助。

+0

我也这样做过,但没有叫! –

+0

你把断点在binder.registerCustomEditor(Ob ....或setValue(text); – krishnakumarp

+0

我在方法中使用System.Out void setAsText() –

2

野生刺伤在黑暗中,它是从​​导入正确的@InitBinder