我有一些输入字段,它们是一个对象的Arraylist的成员。即下面的代码在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>
显示错误,但错误显示在此页面字段的每个文本字段中。
有什么想法吗?
答案 0 :(得分:0)
第二种方法,
<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行为。
没有直接的方法可以按照您期望的方式处理错误。
解决方案是创建一个自定义的Validator for Collection(对象列表)和一个@ControllerAdvice,以在WebDataBinders中注册该Validator。
Validator:
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);
}
}
}
ControllerAdvice:
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));
}
}