我想验证我的输入,但我无法使其工作:页面上没有任何内容。
我的项目在 java 5 中,所以没有 JSR303 (@Valid)。如果我没记错的话,我唯一的解决方案是使用 BindingResult。
我的 Controller :
@Controller
public class MyController {
@RequestMapping(method = RequestMethod.POST, value = "myPage.html")
public void myHandler(MyForm myForm, BindingResult result, Model model) {
result.reject("field1", "error message 1");
}
}
我的jsp:
<form:form commandName="myForm" method="post">
<label>Field 1 : </label>
<form:input path="field1" />
<form:errors path="field1" />
<input type="submit" value="Post" />
</form:form>
我错过了什么?
谢谢 !
最佳答案
BindingResult.reject()
将错误信息与表单整体关联,可以通过<form:errors/>
显示没有 path
.要将错误与特定表单字段相关联,请使用 BindingResult.rejectValue()
:
result.rejectValue("field1", "messageCode", "Default error message");
Java 5 中的 JSR-303 也没有问题。您需要在类路径中以及 Java 6 中使用 JSR-303 提供程序和 API 库。
关于 Spring 3 MVC : problem with form:errors and bindingresult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4670590/