简体   繁体   English

在Spring中,如何指定BindingResult实现

[英]In Spring, how do you specify the BindingResult implementation

I would like to specify the implementing type of BindingResult within a controller. 我想在控制器中指定BindingResult的实现类型。 Is there a way to do that? 有没有办法做到这一点? At the moment, it appears that Spring must determine the implementing type itself (which happens to be a BeanPropertyBindingResult). 目前看来,Spring必须自己确定实现类型(恰好是BeanPropertyBindingResult)。 I suspect there is either a configuration that I'm missing somewhere, or I just need to specify the actual type in the Controller's method signature. 我怀疑某个地方缺少某个配置,或者只需要在Controller的方法签名中指定实际类型。

Example: 例:

/**
 * {@inheritDoc}
 */
@Override
public ModelAndView continue(@ModelAttribute("model") @Valid final T model, final BindingResult results) { ... }

You don't need a custom BindingResult for what you want to do. 您无需为要执行的操作自定义BindingResult You need to implement a BindingErrorProcessor and use it in the WebDataBinder . 您需要实现BindingErrorProcessor并在WebDataBinder中使用它。 It is responsible for adding the errors to the BindingResult and will allow you to use your custom Errors implementation. 它负责将错误添加到BindingResult中 ,并允许您使用自定义的Errors实现。 Here is how you would use it in your controller... 这是您在控制器中使用它的方式...

@Controller
public class MyFormController {
    ...

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setBindingErrorProcessor(new MyCustomBindingErrorProcessor());
    }
}

Mixing binding and validation is bad practice, classes should have a single responsibility. 混合绑定和验证是不好的做法,类应该承担单一责任。 You should not be doing validation in the BindingResult itself. 您不应该在BindingResult本身中进行验证。

You have to add ModelAndView in front of @ModelAttribute like: 您必须在@ModelAttribute前面添加ModelAndView,例如:

public ModelAndView continue(ModelAndView model, @ModelAttribute("model") ObjectType objectName){
}

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

相关问题 如何使用spring指定可以动态更改的数据源 - How do you specify a datasource with spring that you can change dynamically Spring 3 web请求拦截器 - 我如何获得BindingResult? - Spring 3 web request interceptor - how do I get BindingResult? Spring在配置中指定实现 - Spring specify implementation in configuration Spring - 如何从BindingResult中删除`FieldError`? - Spring - How to remove a `FieldError` from a BindingResult? Spring Boot:如何在 application.properties 中指定带有破折号的环境变量? - Spring Boot: How do you specify an environment variable that has dashes in the application.properties? 如何在Spring Data自定义方法实现中使用类型参数? - How do you use type arguments in a Spring Data custom method implementation? 如何指定抽象类的具体实现的哪个版本在Spring中自动装配? - How to specify which version of a concrete implementation for an abstract class to autowire in Spring? 如何在BindingResult中找到错误原因 - how do I locate cause of errors in BindingResult 你如何在 Java 中指定字节文字? - How do you specify a byte literal in Java? 为什么 Spring BindingResult 或验证器不显示任何错误? - Why Spring BindingResult or validator do not show any errors?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM