Spring MVC和@Validate:仅在特定条件下执行验证或者如果用户更改了属性

时间:2015-05-26 19:21:20

标签: spring validation spring-mvc

控制器方法期待@NotNull @Valid @ModelAttribute PersonPerson有一个@Valid Address address属性。

On PersonController.create(@NotNull @Valid @ModelAttribute Person person, BindingResult bindingResult...)我需要仅在用户设置地址的任何字段或基于person实例的字段值(例如person.hasAddress = true)时验证person.address

问题是默认情况下spring会创建一个新的Address实例,该实例在createForm上提交并在验证时失败。

我在Person中创建了一个crossproperty验证,如果hasAddress = true,则要求地址不为null,但无法解决地址字段中验证的问题。

我尝试使用@InitBinder("address") / @InitBinder("person.address")来设置binder.setAutoGrowNestedPaths(false);但我无法接听此电话。全局使用@InitBinder会导致其他属性出现其他问题。

我正在考虑群组,但只有当您在开发时知道您是否要进行验证时才可以使用。在我的情况下,它会在提交时知道,基于地址或hasAddress字段的任何更改

有什么想法吗?

3 个答案:

答案 0 :(得分:3)

In有一个类似的问题(JSR-303 / Spring MVC - validate conditionally using groups

我的解决方案的主要思想是动态绑定数据,即逐步有条件地绑定并验证输入数据:

  1. 我创建了一个新的注记类@BindingGroup。它类似于验证约束注释的groups参数。在我的解决方案中,您使用它来指定一组没有验证约束的字段。

  2. 我创建了一个名为GroupAwareDataBinder的自定义绑定器。调用此绑定程序时,将传递一个组,并且绑定程序仅绑定属于该组的字段。要为字段设置组,您可以使用新的@BindingGroup注释。由于也可能存在正常组足够的情况,绑定器还会查找验证约束的groups参数。为方便起见,活页夹提供了方法bindAndValidate()

  3. 指定名为BasicCheck的绑定组和第二个绑定组AddressCheck,并将它们分配给Person和Address类的相应字段。

  4. 现在,您可以在控制器方法中逐步执行数据绑定。这是一些伪代码:

    //create a new binder for a new Person instance
    result = binder.getBindingResult();
    binder.bindAndValidate(data, BasicCheck.class);
    if (person.hasAddress)
       binder.bindAndValidate(data, AddressCheck.class);
    if (!result.hasErrors())
       // do something
    
  5. 正如您所看到的,缺点是您必须自己执行绑定,而不是使用漂亮的注释。

    这是我的源代码:

    BindingGroup:

    import java.lang.annotation.*;
    
    @Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface BindingGroup
    {
       Class<?>[] value() default {};
    }
    

    在我的情况下,我使用portlet。我认为可以很容易地将绑定器改为servlet:

      import org.springframework.beans.BeanWrapper;
      import org.springframework.beans.MutablePropertyValues;
      import org.springframework.beans.PropertyAccessorUtils;
      import org.springframework.beans.PropertyValue;
      import org.springframework.validation.BindException;
      import org.springframework.web.bind.WebDataBinder;
      import org.springframework.web.portlet.bind.PortletRequestBindingException;
      import org.springframework.web.portlet.bind.PortletRequestParameterPropertyValues;
    
      import javax.portlet.PortletRequest;
      import javax.validation.Constraint;
      import java.beans.PropertyDescriptor;
      import java.lang.annotation.Annotation;
      import java.lang.reflect.InvocationTargetException;
      import java.lang.reflect.Method;
      import java.security.AccessController;
      import java.security.PrivilegedActionException;
      import java.security.PrivilegedExceptionAction;
    
      /**
       * binds only fields which belong to a specific group. Fields annotated with either the
       * {BindingGroup} annotation or with validation-constraints having the "groups"-
       * parameter set.
       * Allows conditional or wizard-like step by step binding.
       *
       * @author Uli Hecht (uli.hecht@gmail.com)
       */
      public class GroupAwarePortletRequestDataBinder extends WebDataBinder
      {
         /**
          * Create a new PortletRequestDataBinder instance, with default object name.
          * @param target the target object to bind onto (or {@code null}
          * if the binder is just used to convert a plain parameter value)
          * @see #DEFAULT_OBJECT_NAME
          */
         public GroupAwarePortletRequestDataBinder(Object target) {
            super(target);
         }
    
         /**
          * Create a new PortletRequestDataBinder instance.
          * @param target the target object to bind onto (or {@code null}
          * if the binder is just used to convert a plain parameter value)
          * @param objectName the name of the target object
          */
         public GroupAwarePortletRequestDataBinder(Object target, String objectName) {
            super(target, objectName);
         }
    
         public void bind(PortletRequest request, Class<?> group) throws Exception
         {
            MutablePropertyValues mpvs = new PortletRequestParameterPropertyValues(request);
            MutablePropertyValues targetMpvs = new MutablePropertyValues();
            BeanWrapper bw = (BeanWrapper) this.getPropertyAccessor();
            for (PropertyValue pv : mpvs.getPropertyValues())
            {
               if (bw.isReadableProperty(PropertyAccessorUtils.getPropertyName(pv.getName())))
               {
                  PropertyDescriptor pd = bw.getPropertyDescriptor(pv.getName());
    
                  for (final Annotation annot : pd.getReadMethod().getAnnotations())
                  {
                     Class<?>[] targetGroups = {};
                     if (BindingGroup.class.isInstance(annot))
                     {
                        targetGroups = ((BindingGroup) annot).value();
                     }
                     else if (annot.annotationType().getAnnotation(Constraint.class) != null)
                     {
                        try
                        {
                           final Method groupsMethod = annot.getClass().getMethod("groups");
                           groupsMethod.setAccessible(true);
                           try {
                              targetGroups = (Class<?>[]) AccessController.doPrivileged(new PrivilegedExceptionAction<Object>()
                              {
                                 @Override
                                 public Object run() throws Exception
                                 {
                                    return groupsMethod.invoke(annot, (Object[]) null);
                                 }
                              });
                           }
                           catch (PrivilegedActionException pae) {
                              throw pae.getException();
                           }
                        }
                        catch (NoSuchMethodException ignored) {}
                        catch (InvocationTargetException ignored) {}
                        catch (IllegalAccessException ignored) {}
                     }
                     for (Class<?> targetGroup : targetGroups)
                     {
                        if (group.equals(targetGroup))
                        {
                           targetMpvs.addPropertyValue(mpvs.getPropertyValue(pv.getName()));
                        }
                     }
                  }
               }
            }
            super.bind(targetMpvs);
         }
    
         public void bindAndValidate(PortletRequest request, Class<?> group) throws Exception
         {
            bind(request, group);
            validate(group);
         }
    
         /**
          * Treats errors as fatal.
          * <p>Use this method only if it's an error if the input isn't valid.
          * This might be appropriate if all input is from dropdowns, for example.
          * @throws org.springframework.web.portlet.bind.PortletRequestBindingException subclass of PortletException on any binding problem
          */
         public void closeNoCatch() throws PortletRequestBindingException
         {
            if (getBindingResult().hasErrors()) {
               throw new PortletRequestBindingException(
                     "Errors binding onto object '" + getBindingResult().getObjectName() + "'",
                     new BindException(getBindingResult()));
            }
         }
      }
    

    以下是控制器方法应如何开始的示例。如果使用正常的绑定机制,则需要一些额外的步骤,通常由Spring完成。

      @ActionMapping
      public void onRequest(ActionRequest request, ActionResponse response, ModelMap modelMap) throws Exception
      {
         Person person = new Person();
         GroupAwarePortletRequestDataBinder dataBinder =
               new GroupAwarePortletRequestDataBinder(person, "person");
         webBindingInitializer.initBinder(dataBinder, new PortletWebRequest(request, response));
         initBinder(dataBinder);
         BindingResult result = dataBinder.getBindingResult();
         modelMap.clear();
         modelMap.addAttribute("person", Person);
         modelMap.putAll(result.getModel());
    
         // now you are ready to use bindAndValidate()
       }
    

    Person类字段的一些示例:

    @NotNull(groups = BasicCheck.class)
    public String getName() { return name; }
    
    @BindingGroup(BasicCheck.class)
    public String phoneNumber() { return phoneNumber; }
    
    @Valid
    public Address getAddress() { return address; }
    

    地址类:

    @BindingGroup(BasicCheck.class)
    public Integer getZipCode() { return zipCode; }
    

    写这个答案是很多工作,所以我希望它可以帮助你。

答案 1 :(得分:1)

  1. 从地址栏
  2. 中删除了@Valid
  3. 手动验证:在控制器的create方法内:validateAddressIfNeeded(person, bindingResult)

    private void validateAddressIfNeeded(Person person, BindingResult bindingResult) {
        if (person.hasAddress()) {
            bindingResult.pushNestedPath("address");
            validator.validate(person.getAddress(), bindingResult);
            bindingResult.popNestedPath();
        }
    }
    

答案 2 :(得分:0)

这个答案适用于@InitBinder部分。 Javadoc说,注释的值是这个init-binder方法应该适用的[model属性]的名称。

我认为您应该明确地为@InitBinder

的模型属性和同名设置名称
@InitBinder("person_with_address")
public void ...
...
public String create(@NotNull @Valid @ModelAttribute("person_with_address") Person person, BindingResult bindingResult...)

这样你就可以为该控制器方法使用专用绑定。