[英]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.