java spring initBinder 和 webbindinginitializer 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/992097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:43:06  来源:igfitidea点击:

spring initBinder and webbindinginitializer example

javaspringspring-mvc

提问by cometta

I read few books on spring2.5 on these topic, but still cannot grab the concepts when to use @initBinder. can anyone share any reference or explain in what situation i can use this on web application? How propertyEditor relate to it?

我读了几本关于这些主题的 spring2.5 书籍,但仍然无法掌握何时使用 @initBinder 的概念。任何人都可以分享任何参考资料或解释在什么情况下我可以在网络应用程序上使用它?propertyEditor 与它有什么关系?

回答by skaffman

Well I can't really put it any better than the books, but if your controller has any public methods annotated with @InitBinder, then these methods will be called by the container just before each request is processed, passing in the WebDataBinder being used by the framework.

好吧,我真的不能说它比书本上更好,但是如果您的控制器有任何用 @InitBinder 注释的公共方法,那么容器将在处理每个请求之前调用这些方法,并传入正在使用的 WebDataBinder框架。

The most common reason to do this is when you want to customise the way that Spring tries to bind request parameters on to your model, for example if your model has custom datatypes that Spring can't handle out of the box. You register your PropertyEditors against the WebDataBinder. A trivial example would be if you use the JodaTime library in your model, and you want to bind timestamp strings on to a Joda DateTime object.

这样做的最常见原因是当您想要自定义 Spring 尝试将请求参数绑定到您的模型的方式时,例如,如果您的模型具有 Spring 无法立即处理的自定义数据类型。您针对 WebDataBinder 注册您的 PropertyEditor。一个简单的例子是,如果您在模型中使用 JodaTime 库,并且希望将时间戳字符串绑定到 Joda DateTime 对象。

With Spring 2.0, you use to have to override the protected initBinder() method from the controller superclass, but Spring 2.5 removes the need to do that, you can just use annotations now.

在 Spring 2.0 中,您过去必须从控制器超类中覆盖受保护的 initBinder() 方法,但 Spring 2.5 不再需要这样做,您现在只需使用注释即可。

回答by cometta

Another reason beside what skaffman mentioned, would be to set a custom validator on your WebDataBinder. What I will usually do is use JSR-303 bean validation, and then bind a validator that provides additional validation not provided by JSR-303.

除了 skaffman 提到的,另一个原因是在您的 WebDataBinder 上设置自定义验证器。我通常会做的是使用 JSR-303 bean 验证,然后绑定一个验证器,该验证器提供 JSR-303 未提供的附加验证。

Inside your controller:

在您的控制器内部:

@InitBinder
protected void initBinder(WebDataBinder webDataBinder) {
    Validator validator = webDataBinder.getValidator();
    webDataBinder.setValidator(new UserFormValidator(validator));
}

What I'm doing is taking in the bean validator, calling that inside my custom validator, and then calling my custom validations. Something like this:

我正在做的是接收 bean 验证器,在我的自定义验证器中调用它,然后调用我的自定义验证。像这样的东西:

public class UserFormValidator implements Validator {

    private Validator validator;

    public AuthUserFormValidator(Validator validator) {
        this.validator = validator;
    }

    @Override
    public boolean supports(Class<?> clazz) {
        return UserForm.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {

        // Run the bean validation...

        validator.validate(target, errors);

        // Do your custom validation on userForm here...

        UserForm userForm = (UserForm) target;

        // Validation on userForm...
    }
}

回答by Valeri Shibaev