[英]InitBinder is not called/Invoked
我在JSP页面上有单选按钮。
<form:form modelAttribute="obj" action="save">
<form:radiobuttons path="person" items="${List}"/>
<form:button>Save</form:button>
</form:form>
列表:人员列表对象对象:
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获取人员对象字符串的。所以,我正在获取无法从String转换为Person的绑定异常。
将数据绑定到modelAttribute之前,将调用InitBinder。因此,在initbinder中,我会将字符串转换为Person对象。
此处的主要问题是My InitBinder在“未调用/已调用”中 。 请给我解决方案。
谢谢。
请通过放置断点或调试语句来检查是否正在调用它。
您的模型属性名为“ obj”。 但是在属性编辑器中,您已经使用“人”作为属性路径。
请尝试将“ obj”用作propertyPath或删除该参数并使用此签名。 registerCustomEditor(Class requiredType,PropertyEditor propertyEditor类)
希望能帮助到你。
黑暗中的狂野刺杀,是否从org.springframework.web.bind.annotation
导入正确的@InitBinder
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.