java 未显示 Spring 验证错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2464301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring validation errors not displayed
提问by Art Vandelay
I have the following situation. I have a validator to validate my command object and set the errors on a Errors object to be displayed in my form. The validator is invoked as expected and works okay, but the errors i set on the Errors objects are not displayed, when i am sent back to my form because of the validation errors.
我有以下情况。我有一个验证器来验证我的命令对象并设置错误对象上的错误以显示在我的表单中。验证器按预期调用并且工作正常,但是我在 Errors 对象上设置的错误没有显示,当我因为验证错误而被发送回我的表单时。
Validator:
验证器:
public void validate(Object obj, Errors err) {
MyCommand myCommand = (MyCommand) obj;
int index = 0;
for (Field field : myCommand.getFields()) {
if (field.isChecked()) {
if ((field.getValue() == null) || (field.getValue().equals(""))) {
err.rejectValue("fields[" + index + "].value", "errors.missing");
}
}
index++;
}
if (myCommand.getLimit() < 0) {
err.rejectValue("limit", "errors.invalid");
}
}
Command:
命令:
public class MyCommand {
private List<Field> fields;
private int limit;
//getters and setters
}
public class Field {
private boolean checked;
private String name;
private String value;
//getters and setters
}
Form:
形式:
<form:form id="myForm" method="POST" action="${url}" commandName="myCommand">
<c:forEach items="${myCommand.fields}" var="field" varStatus="status">
<form:checkbox path="fields[${status.index}].checked" value="${field.checked}" />
<c:out value="${field.name}" />
<form:input path="fields[${status.index}].value" />
<form:errors path="fields[${status.index}].value" cssClass="error" /></td>
<form:hidden path="fields[${status.index}].name" />
</c:forEach>
<fmt:message key="label.limit" />
<form:input path="limit" />
<form:errors path="limit" cssClass="error" />
</form:form>
Controller:
控制器:
@RequestMapping(value = REQ_MAPPING, method = RequestMethod.POST)
public String onSubmit(Model model, MyCommand myCommand, BindingResult result) {
// validate
myCommandValidator.validate(myCommand, result);
if (result.hasErrors()) {
model.addAttribute("myCommand", myCommand);
return VIEW;
}
// form is okay, do stuff and redirect
}
Could it be that the paths i give in the validator and tag are not correct? The validator validates a command object containing a list of objects, so that's why i give a index on the list in the command object when registering an error message (for example: "fields["+index+"]".value). Or is it that the Errors object containing the errors is not available to my view?
是不是我在验证器和标签中给出的路径不正确?验证器验证包含对象列表的命令对象,这就是为什么我在注册错误消息时在命令对象中的列表上给出索引(例如:“fields[”+index+“]”.value)。还是包含错误的 Errors 对象在我的视图中不可用?
Any help is welcome and appreciated, it might give me a hint or point me in right direction.
欢迎和感谢任何帮助,它可能会给我一个提示或指向正确的方向。
回答by Yuri.Bulkin
I found what is your problem.
property fieldsin object myCommandalways null.
You need to create constructor for class MyCommand in which you need to use LazyListfrom Apache Commons or AutoPopulatingListfrom Spring to create auto growing list of Field
我发现你的问题是什么。fields对象中的属性myCommand始终为空。您需要为类 MyCommand 创建构造函数,您需要在其中使用LazyListApache Commons 或AutoPopulatingListSpring 来创建自动增长的列表Field
In example (using AutoPopulatingList):
在示例中(使用 AutoPopulatingList):
public class MyCommand {
private List<Field> fields;
private int limit;
//getters and setters
//...
//Constructor
public MyCommand() {
fields = new AutoPopulatingList<Field>(Field.class);
}
}
回答by Hans Westerbeek
The line err.rejectValue("fields[" + index + "].value", "errors.missing");will not do what you are trying to achieve. The first argument must be a property name of your Command bean, in your case fields.
该生产线err.rejectValue("fields[" + index + "].value", "errors.missing");不会执行您想要实现的目标。第一个参数必须是您的命令 bean 的属性名称,在您的案例字段中。
To give the user a message you will have to use a parameterizable message in your properties file, eg. myform.field.error = you have an error in field {0}
要向用户发送消息,您必须在属性文件中使用可参数化的消息,例如。 myform.field.error = you have an error in field {0}
So, use this method of the Errors object:
所以,使用 Errors 对象的这个方法:
void rejectValue(String field, String errorCode, Object[] errorArgs, String defaultMessage)
Where you pass indexinto errorArgs
当你传递index到errorArgs

