简体   繁体   English

spring initBinder和webbindinginitializer示例

[英]spring initBinder and webbindinginitializer example

I read few books on spring2.5 on these topic, but still cannot grab the concepts when to use @initBinder. 关于这些主题,我在spring2.5上读了几本书,但是仍然无法抓住使用@initBinder时的概念。 can anyone share any reference or explain in what situation i can use this on web application? 任何人都可以分享任何参考或解释在什么情况下我可以在Web应用程序上使用它? How propertyEditor relate to it? propertyEditor如何与它相关?

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. 执行此操作的最常见原因是,您希望自定义Spring尝试将请求参数绑定到模型的方式,例如,如果您的模型具有Spring无法处理的自定义数据类型。 You register your PropertyEditors against the WebDataBinder. 您对WebDataBinder注册PropertyEditors。 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. 如果您在模型中使用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无需执行此操作,您现在只需使用注释即可。

Another reason beside what skaffman mentioned, would be to set a custom validator on your WebDataBinder. 除了skaffman提到的另一个原因是在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. 我通常会做的是使用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. 我正在做的是接受bean验证器,在我的自定义验证器中调用它,然后调用我的自定义验证。 Something like this: 像这样的东西:

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...
    }
}

它需要Spring 2.5.1+,请参阅https://jira.springsource.org/browse/SPR-4182

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM