【发布时间】:2012-05-23 04:57:11
【问题描述】:
我正在将应用程序从 Spring 2.0.7 迁移到 3.1.1,但遇到了 initBinder 问题。我们以前的方法看起来像
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
MyCommand command = (MyCommand)binder.getTarget();
binder.registerCustomEditor(CustomerCard.class, createEditorFromCommand(command));
}
PropertyEditor 使用目标的位置。当我将它设置为带注释的 Controller 时不再调用此方法,因此我添加了 @InitBinder 注释:
@InitBinder
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
MyCommand command = (MyCommand)binder.getTarget();
binder.registerCustomEditor(CustomerCard.class, createEditorFromCommand(command));
}
不幸的是,binder.getTarget() 只是一些默认对象。 @InitBinder 的文档还指出我也无法将该命令作为参数:
这样的 init-binder 方法支持所有 {@link RequestMapping} 支持,除了命令/表单对象和 对应的验证结果对象。
这样做的正确方法是什么?
【问题讨论】: