【发布时间】:2018-03-26 18:01:44
【问题描述】:
我在使用 Spring 进行休眠验证时遇到了一些问题。我按照在线教程中的说明做了所有事情,但它不起作用,我只是转到下一页而没有验证错误。
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class Customer {
private String firstName;
@NotNull()
@Size(min=1, message = "this field must not to be empty")
private String lastName;
控制器:
@RequestMapping("/processForm")
public String processForm(@ModelAttribute("customer") @Valid Customer
customer, BindingResult bindingResult) {
if(bindingResult.hasErrors()) {
return "customer-form";
}
return "customer-confirmation";
}
customer-form.jsp
<form:form action="processForm.form" modelAttribute="customer">
First name: <form:input path="firstName"/>
<br>
Last name (*): <form:input path="lastName"/>
<form:errors path="lastName" cssClass="error"/>
<input type="submit" value="Submit"/>
</form:form>
因此,当我的 lastName 字段为空时,BindingResult 中没有错误。我做错了什么?
【问题讨论】:
-
您是在执行 HTTP GET 还是 POST?
-
这是POST方法
-
你的控制器方法 processForm() 被调用了吗?如果您执行 HTTP 发布,则应使用
@RequestMapping(value = "/processForm", method = RequestMethod.POST)进行注释 -
顺便说一句:
@NotNull() @Size(min=1, message = "this field must not to be empty")可以替换为@NotEmpty -
这里有点晚了。你解决了吗?如果没有,你确定你的类路径中有
hibernate-validator吗?
标签: java spring validation spring-mvc hibernate-validator