有人可以帮忙解决这个问题吗? 我无法在 spring 项目中配置验证。
实体类
public class Employee {
@Size(min = 2,message = "Name must be minimum 2 symbols")
private String name;
@NotEmpty(message = "surname is required field")
private String surname;
}
控制器类
@RequestMapping("/showDetails")
public String showDetails(
@ModelAttribute("employee") @Valid Employee employee,
BindingResult bindingResult, Errors errors){
if(bindingResult.hasErrors()){ // bindingResult.hasErrors() always return false
return "ask-detail-view";
}else{
return "show-detail-view";
}
}
pom.xml
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>8.0.1.Final</version>
</dependency>
jsp页面
<from:form action="showDetails" modelAttribute="employee">
Name<form:input path="name"/>
<form:errors path="name" />
<br>
<br>
Surname<form:input path="surname"/>
<form:errors path="surname" />
<input type="submit">
</from:form>
如何正确配置?
@Valid注解已经提供了相应的响应。因此,如果您不需要函数中的确切错误,这应该足够了。