我正在将应用程序从 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 使用。当我将其设置为带注释的控制器时,不再调用此方法,因此我添加了@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} 支持的所有参数,除了命令/表单对象和相应的验证结果对象。
这样做的正确方法是什么?