gpt4 book ai didi

Spring MVC 和 @Validate : Perform validate only on specific condition or if user changes the property

转载 作者:行者123 更新时间:2023-12-02 21:16:15 25 4
gpt4 key购买 nike

Controller 方法需要一个@NotNull @Valid @ModelAttribute PersonPerson 具有 @Valid Address 地址 属性。

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

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

我在 Person 中创建了一个跨属性验证,它要求在 hasAddress=true 的情况下地址不为空,但无法通过地址字段中的验证解决问题。

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

我正在考虑组,但只有当您在开发时间知道是否不需要验证时才能使用组。就我而言,根据地址或 hasAddress 字段的任何更改,在提交时就会知道

有什么想法吗?

最佳答案

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

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

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

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

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

  4. 现在您可以在 Controller 方法中逐步执行数据绑定(bind)。这是一些伪代码:

    //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

正如您所看到的,缺点是您必须自己执行绑定(bind),而不是使用漂亮的注释。

这是我的源代码:

绑定(bind)组:

import java.lang.annotation.*;

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

就我而言,我使用 portlet。我认为可以轻松地为 servlet 调整绑定(bind)器:

  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 (<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2c7dedb9cdad7d1dac6f2d5dfd3dbde9cd1dddf" rel="noreferrer noopener nofollow">[email protected]</a>)
*/
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()));
}
}
}

以下是 Controller 方法应如何开始的示例。如果使用正常的绑定(bind)机制,则需要一些额外的步骤,这些步骤通常由 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; }

写这个答案需要大量工作,所以我希望它对您有所帮助。

关于Spring MVC 和 @Validate : Perform validate only on specific condition or if user changes the property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30467124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com