Time for action – adding bean validation support
In this section, you will learn how to validate a form submission in a Spring MVC application. In our project, we have the add products form already. Add some validation to this form by performing the following steps:
Open the
pom.xml
file—you can findpom.xml
under the root directory of the project itself.You will see some tabs at the bottom of the
pom.xml
file. Select the Dependencies tab and click on the Add button in the Dependencies section.A Select Dependency window will appear; enter Group Id as
org.hibernate
, enter Artifact Id ashibernate-validator
, enter Version as4.3.1.Final
, select Scope as compile, and click on the OK button and savepom.xml
.Open the
Product
domain class and add the@Pattern
annotation (javax.validation.constraints.Pattern
) at the top of theproductId
field as follows:@Pattern(regexp="P[0-9]+", message="{Pattern.Product.productId.validation}") private String productId;
Similarly, add the
@Size
,@Min
,@Digits
,...