form:error 消息显示在每个字段(列表)中

form:error message shows up in every field (List)

我有一些输入字段是一个对象的 Arraylist 的成员。 IE。下面的代码在 c:forEach 循环中。

<tr> <td><form:label path="person[${index}].firstname">First name</form:label></td> <td><form:input path="person[${index}].firstname" /></td> <td><form:errors path="person[${index}].firstname" cssClass="error" /></td> </tr>

这样错误就不会出现了。另一方面,这段代码:

<tr> <td><form:label path="person[${index}].firstname">First name</form:label></td> <td><form:input path="person[${index}].firstname" /></td> <td><form:errors path="person.firstname" cssClass="error" /></td> </tr>

显示错误,但错误显示在该页面字段的每个文本字段中。

有什么想法吗?

在你的第二种方法中,

<tr>
     <td><form:label path="person[${index}].firstname">First name</form:label></td>
     <td><form:input path="person[${index}].firstname" /></td>
     <td><form:errors path="person.firstname" cssClass="error" /></td>
</tr>

Spring 会将属性的错误消息与您映射的每个文本框绑定 <form:errors with path = person.firstname> 这是正确的 spring 行为。

没有直接的方法以您期望的方式处理错误。

解决方案是为集合(对象列表)创建自定义验证器和在 WebDataBinders 中注册该验证器的@ControllerAdvice。

验证者:

import java.util.Collection;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

/**
 * Spring {@link Validator} that iterates over the elements of a 
 * {@link Collection} and run the validation process for each of them
 * individually.
 *   
 * @author DISID CORPORATION S.L. (www.disid.com)
 */
public class CollectionValidator implements Validator {

  private final Validator validator;

  public CollectionValidator(LocalValidatorFactoryBean validatorFactory) {
    this.validator = validatorFactory;
  }

  @Override
  public boolean supports(Class<?> clazz) {
    return Collection.class.isAssignableFrom(clazz);
  }

  /**
   * Validate each element inside the supplied {@link Collection}.
   * 
   * The supplied errors instance is used to report the validation errors.
   * 
   * @param target the collection that is to be validated
   * @param errors contextual state about the validation process
   */
  @Override
  @SuppressWarnings("rawtypes")
  public void validate(Object target, Errors errors) {
    Collection collection = (Collection) target;
    for (Object object : collection) {
      ValidationUtils.invokeValidator(validator, object, errors);
    }
  }
}

控制器通知:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

/**
 * Controller advice that adds the {@link CollectionValidator} to the 
 * {@link WebDataBinder}.
 * 
 * @author DISID CORPORATION S.L. (www.disid.com)
 */
@ControllerAdvice
public class ValidatorAdvice {

  @Autowired
  protected LocalValidatorFactoryBean validator;


  /**
   * Adds the {@link CollectionValidator} to the supplied 
   * {@link WebDataBinder}
   * 
   * @param binder web data binder.
   */
  @InitBinder
  public void initBinder(WebDataBinder binder) {
    binder.addValidators(new CollectionValidator(validator));
  }
}