为什么我的Spring 3 Validator会验证模型上的所有内容? [英] Why is my Spring 3 Validator Validating Everything on the Model?

查看:144
本文介绍了为什么我的Spring 3 Validator会验证模型上的所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有验证器的spring 3控制器,用于其中一种方法.它坚持要验证模型上的每个对象.谁能向我解释为什么这样做或我做错了什么?

I have a spring 3 controller with a validator for one of the methods. It insists on validating every object on the model. Would anyone be able to explain to me why it does this or if I'm doing something wrong?

根据文档5.7.4.3配置供Spring MVC使用的JSR-303验证器(

According to the docs, 5.7.4.3 Configuring a JSR-303 Validator for use by Spring MVC (http://static.springsource.org/spring/docs/3.0.0.RC3/spring-framework-reference/html/ch05s07.html)

对于JSR-303,单个javax.validation.Validator实例通常会验证所有声明验证约束的模型对象.要使用Spring MVC配置支持JSR-303的Validator,只需将JSR-303提供程序(例如Hibernate Validator)添加到您的类路径中. Spring MVC将检测到它并自动在所有Controller中启用JSR-303支持.

With JSR-303, a single javax.validation.Validator instance typically validates all model objects that declare validation constraints. To configure a JSR-303-backed Validator with Spring MVC, simply add a JSR-303 Provider, such as Hibernate Validator, to your classpath. Spring MVC will detect it and automatically enable JSR-303 support across all Controllers.

示例:

@Controller
public class WhaleController {

        @Autowired
        private Validator myValidator;

        @Autowired
        private WhaleService whaleService;

        @InitBinder
        protected void initBinder(WebDataBinder binder) {
                binder.setValidator(this.myValidator);
        }

        @RequestMapping(value="/save-the-whales")
        @Transactional
        public void saveTheWhales(@Valid WhaleFormData formData, BindingResult errors, Model model) {
                if (!errors.hasFieldErrors()) {
                        Whale whale = new Whale();

                        whale.setBreed( formData.getBreed() );

                        this.whaleService.saveWhale( whale );

                        model.addAttribute("whale", whale);

                }
                model.addAttribute("errors", errors.getFieldErrors());
        }

}

在运行时,它将抱怨Whale是myValidator的无效目标(设置为验证WhaleFormData,并且效果很好).鲸鱼是一个POJO,没有任何验证约束,注释和任何位置都没有配置.通过反复试验,我发现放置在模型上的任何对象都将尝试进行验证,如果未设置验证程序来处理该对象,则该对象将失败.原始函数就可以了.

When run it will complain that Whale is an invalid target for myValidator (which is set to validate WhaleFormData, and does so fine). Whale is a POJO with no validation constraints, annotation and no config anywhere. Through trial and error I've found that ANY object placed on the model will attempt to be validated and fail if the validator is not setup to handle it. Primitives are just fine.

谁能告诉我为什么,请给我指出适当的文档和/或告诉我在不进行验证的情况下将某些东西放在模型上的最佳方法吗?

Can anyone tell me why this is, point me to the appropriate documentation and/or tell me the best way to put something on the model without having it validated?

在上述情况下,我想在模型上放置鲸鱼",因为它现在具有从我的持久层接收到的唯一的whaleId().

In the case above I would like to place "whale" on the model as it will now have a unique whaleId() that it received from my persistence layer.

谢谢!

推荐答案

我猜这种行为在文档中没有涵盖.

I guess this behaviour is not covered in the documentation well.

此问题是由以下原因引起的:

The problem is caused by the following:

  1. 默认情况下,传入和传出的每个非原始模型属性都调用@InitBinder注释的方法(将其用于传出服装的目的是允许您注册自定义PropertyEditor,呈现表单时由表单标签使用的标签.)

  1. By default, @InitBinder-annotated method is called for each non-primitive model attribute, both incoming and outcoming (the purpose of calling it for outcoming attibutes is to allow you to register custom PropertyEditors, which are used by form tags when rendering a form).

DataBinder.setValidator()包含一个防御检查,该防御检查调用Validator.supports()并在返回false时引发异常.因此,没有试图执行验证的尝试,只是尽早检查.

DataBinder.setValidator() contains a defensive check that call Validator.supports() and throws an exception if false is returned. So, there is no attempt to perform a validation, just an early check.

解决方案是将@InitBinder的范围限制为特定属性:

The solution is to restrict the scope of @InitBinder to particular attribute:

@InitBinder("whaleFormData")
protected void initBinder(WebDataBinder binder) { ... }

这篇关于为什么我的Spring 3 Validator会验证模型上的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆