- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Controller 方法需要一个@NotNull @Valid @ModelAttribute Person
。Person
具有 @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)和验证输入数据:
我创建了一个新的注释类@BindingGroup
。它类似于验证约束注释的 groups 参数。在我的解决方案中,您使用它来指定一组没有验证约束的字段。
我创建了一个名为 GroupAwareDataBinder 的自定义绑定(bind)器。当调用此绑定(bind)器时,会传递一个组,并且绑定(bind)器仅绑定(bind)属于该组的字段。要为字段设置组,您可以使用新的 @BindingGroup
注释。由于也可能存在正常组就足够的情况,绑定(bind)器还会查找验证约束的组参数。为了方便起见,绑定(bind)器提供了一个方法bindAndValidate()
。
指定一个名为 BasicCheck
的绑定(bind)组和第二个绑定(bind)组 AddressCheck
,并将它们分配给您的 Person 和 Address 类的相应字段。
现在您可以在 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/
export class UserListComponent implements OnInit{ users; constructor(private userService: UserS
我最近在我的系统中遇到了 Java 语言环境的问题,我试图用这个配置运行一个项目: -Duser.language=pt_BR -Duser.country=BR 谷歌搜索后,我找到了this sit
1 当我希望出现注册错误时,我的代码出现问题:管理器不可用; 'auth.User' 已替换为 'users.User' ,我尝试解决其他问题,与 Manager 不可用相同; 'auth.User'
Loopback 非常酷,但这是我迄今为止遇到的一个缺点,我真的不确定如何解决它。内置用户模型在我的 MongoDB 数据库中生成一个名为“User”的集合,当我尝试根据 Loopback.js 自己
我在 aws cognito 中有以下用户组。行政成员付费成员(member) 我想在所有用户注册我的应用程序时将所有用户默认分配到 Member 用户组,这样我就可以为该用户组分配不同的 IAM A
blogsIndex.blade.php @extends('layouts.default') @section('details')
我正在尝试在Rails 3开发环境中使用sqlite3而不是MySQL,但是遇到了问题。尝试执行rake db:migrate时,我得到: SQLite3::SQLException: no such
尝试使用 构建 API Phoenix v1.3 按照本教程: https://dreamconception.com/tech/phoenix-full-fledged-api-in-five-mi
我正在使用通过模板 cookie-cutter 创建的 Django。当我尝试在本地使用 docker 运行项目时,出现以下错误。 FATAL: password authentication fai
我正在尝试使用 node.js/adonis 创建新用户 我创建了这两个函数: const User = use("App/Models/User") async store ({ request,
我想安排一些事情,例如 GET 请求 http://example.com/user/foo@bar.com 内部调用脚本 /var/www/example.com/rest/user/GET.php
我是一名具有可用性工程背景的软件开发人员。当我在研究生院学习可用性工程时,其中一位教授有一句口头禅:“你不是用户”。我们的想法是,我们需要将 UI 设计基于实际的用户研究,而不是我们自己关于 UI 应
您好,我正在制作一个使用互联网发送消息的消息传递应用程序。我需要从用户 a 向用户 b 发出通知。 我使用这段代码: if (toUser!= nil){ parseMessage[@
在 ruby/ror 中你可以这样做: user = User.new(params[:user]) 它使用发布表单中的值填充新对象。 使用 django/python 可以完成类似的事情吗? 最
每当我编辑用户的角色时,用户都需要注销并重新登录以查看更改。提升用户时没有问题,因为他们在再次登录之前不会看到额外的权限。但是,当降级发生时,用户仍将保留其现有角色,这会带来安全风险。想象一下,撤销一
我的核心数据有线问题。使用 iOS 10 中的 Swift3,每次使用 获取或存储数据时,我都会获得托管对象上下文 func getContext () -> NSManagedObjectCont
我发现当我使用 users_path(user) 时它返回 /users.id 其中 id 是用户的 ID 但我希望它返回 /用户/ID。我的配置 routes.rb 如下所示。 # config/r
我的应用程序在我的测试设备上正常运行(当我通过 ADT 安装它时,当我通过导出的 APK 文件安装它时)但它在 Play Store 测试设备上失败并出现以下错误: Permission Denial
创建模型的第一个条目会抛出错误 我执行了以下命令进行迁移 manage.py makemigrations manage.py migrate 在我执行这些命令以在数据库中创建第一个“数据”之后,一切
我正在尝试实现一个 getter,但它在下面代码 fragment 的最后一行向我显示了这个错误。 代码是—— class AuthRepository extends BaseAuthReposit
我是一名优秀的程序员,十分优秀!