0

我在 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 获取 person 对象字符串...所以我得到绑定异常,无法从 String 转换为 Person

在将数据绑定到 modelAttribute 之前调用 InitBinder。因此在 initbinder 我会将字符串转换为 Person 对象。

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

谢谢你。

4

2 回答 2

2

Wild stab in the dark, is it importing the correct @InitBinder from org.springframework.web.bind.annotation?

于 2013-10-16T14:48:03.090 回答
2

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

您的模型属性名为“obj”。但在 propertyeditor 中,您使用“person”作为属性路径。

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

希望能帮助到你。

于 2013-08-29T05:04:13.510 回答