问题
I have some problems with hibernate validations with 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
<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. What am i doing wrong?
回答1:
Add hibernate-validator
in your classpath if it does not exist already. If you are using any build tool like gradle
or maven
just add hibernate-validator
to dependencies.
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>
回答2:
Note: solution based on the 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).
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.
In order to make sure libraries you included are assembled together with the Spring MVC solution do the following:
- go to File > Project Structure > Artifacts (from the left pane)
- expand the WEB-INF/lib directory under Output Layout tab
- highlight lib directory and add corresponding Hibernate artifacts by clicking the + button and selecting Library Files
- after selecting the library from project libraries, select OK, Apply and you are ready to go
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.
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.
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.
回答3:
Add setter to your Customer
class.
Without setter your class does not be populated.
来源:https://stackoverflow.com/questions/49497795/spring-mvc-with-hibernate-validation-doesnt-work