1

我正在尝试使用以下代码将字符串输入表单参数绑定到 bean 的日期属性。我只想将日期格式化程序绑定到名为registrationDatebean的特定属性User。我尝试了各种方法来 registrationDate在调用中指定字段名称,binder.registerCustomEditor但从CustomDateEditor未被调用。

CustomDateEditor仅当我从binder.registerCustomEditor调用中删除字段名称并将其设置为所有日期字段的全局时才有效,如initBinderAll()下面控制器中方法中的最后一个注释行所示。希望有人指出我正确的方向。

用户.jsp:-

<form:form action="/some_url" modelAttribute="user">
    <input type="text" id="registrationDate" name="registrationDate" value="${regDate}" />
</form:form>      

用户.java :-

@Entity
@Table(name="user")
public class User {
    @Column(name = "reg_date")
    @Temporal(TemporalType.DATE)
    private Date registrationDate;
}   

用户控制器.java :-

@Controller
public class UserController {
    @RequestMapping(value = "/some_url", method = RequestMethod.POST)
    public String handleSubmission(
            @ModelAttribute("user")@Valid User user, BindingResult result) throws IOException {
    }

    @InitBinder
    public void initBinderAll(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, "user.registrationDate", new CustomDateEditor(new SimpleDateFormat("yyyy-dd-MM"), false));

        binder.registerCustomEditor(Date.class, "User.registrationDate", new CustomDateEditor(new SimpleDateFormat("yyyy-dd-MM"), false));

        binder.registerCustomEditor(Date.class, "registrationDate", new CustomDateEditor(new SimpleDateFormat("yyyy-dd-MM"), false));

        //binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-dd-MM"), true));
    }
}
4

0 回答 0