[英]Spring MVC with hibernate validation doesn't work
I have some problems with hibernate validations with Spring.我在使用 Spring 进行休眠验证时遇到了一些问题。 I did everything as explained in an online tutorial, but it's not working and I just go to the next page without validation error.我按照在线教程中的说明做了所有事情,但它不起作用,我只是转到下一页而没有验证错误。
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;
Controller:控制器:
@RequestMapping("/processForm")
public String processForm(@ModelAttribute("customer") @Valid Customer
customer, BindingResult bindingResult) {
if(bindingResult.hasErrors()) {
return "customer-form";
}
return "customer-confirmation";
}
customer-form.jsp客户表单.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>
So, there are no errors in BindingResult when I have an empty field for lastName.因此,当 lastName 为空字段时,BindingResult 中没有错误。 What am i doing wrong?我做错了什么?
Add hibernate-validator
in your classpath if it does not exist already.如果hibernate-validator
尚不存在,请在您的类路径中添加它。 If you are using any build tool like gradle
or maven
just add hibernate-validator
to dependencies.如果您使用任何构建工具,如gradle
或maven
只需将hibernate-validator
添加到依赖项。
For example:例如:
Gradle:摇篮:
compile group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.0.13.Final'
Maven:马文:
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.13.Final</version>
</dependency>
Note: solution based on the IntelliJ IDEA IDE注:基于IntelliJ IDEA IDE的解决方案
If not using build tools such as Gradle or Maven, this could be related to the IDE's behavior during the compilation and build of the solution when manually adding libraries (project dependencies).如果不使用 Gradle 或 Maven 等构建工具,这可能与手动添加库(项目依赖项)时在编译和构建解决方案期间 IDE 的行为有关。
Regardless of having the library files manually included in the project and indexed by the IDE by adding those manually through Project Structure > Libraries, those aren't going to be included automatically in the compilation output of your build process.无论将库文件手动包含在项目中并由 IDE 通过“项目结构”>“库”手动添加这些文件进行索引,这些文件都不会自动包含在构建过程的编译输出中。
In order to make sure libraries you included are assembled together with the Spring MVC solution do the following:为了确保您包含的库与 Spring MVC 解决方案组合在一起,请执行以下操作:
Now, when you rebuild the solution and Run the server, your assembled output (build) will contain all additionally added artifacts (libs) such as Hibernate validation.现在,当您重新构建解决方案并运行服务器时,您的组装输出(构建)将包含所有额外添加的工件(库),例如 Hibernate 验证。
When using IntelliJ IDEA's builder to compile Java-based project, it uses its own project model, configuration and built-in mechanism to assemble the output application , so such steps like I mentioned are required.使用IntelliJ IDEA的builder编译基于Java的项目时,它使用自己的项目模型、配置和内置机制来组装输出应用程序,所以需要像我提到的这些步骤。
This is mandatory here In contrast to compiling and building such Java projects using Gradle or Maven where those use their own underlying build process and run specific tasks to generate the output based on the build.gradle or pom.xml configuration holding all of the config values and dependencies.与使用 Gradle 或 Maven 编译和构建此类 Java 项目相反,这些项目使用自己的底层构建过程并运行特定任务以根据包含所有配置值的 build.gradle 或 pom.xml 配置生成输出和依赖。
Add setter to your Customer
class.将 setter 添加到您的Customer
类。
Without setter your class does not be populated.没有二传手,你的班级不会被填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.