I. Technology
- Java 1.8
- Maven 3.3.9
- Spring Tool Suite – Version 3.8.1.RELEASE
- Spring Boot: 1.5.1.RELEASE
II. Overview
1. Goal
Create a Custom Validation Annotation
@ValidEmail
that helps to validate field String email on server-side:
@ValidEmail(min = 10, message = "Please enter a valid email.")
private String email;
2. Project Structure
-
WebController is a Controller which has request mapping methods to display our page.
-
RequestInfo is a Data Model Class with annotation for validation.
-
ValidEmail is an Annotation Class refers to
ValidEmailValidator class.
-
ValidEmailValidator is a Constraint Class which implements
ConstraintValidator interface.
-
webapp folder contains all necessary
JSP file and static resource.
-
application.properties contains settings for prefix and suffix for JSP files.
-
Dependencies for
Spring Boot,
Tomcat Embed Jasper and
Common Validator in
pom.xml.
3. Step to do
- Create Spring Boot project & add Dependencies
- Create jsp file and static resource
- Set prefix and suffix resolution
- Create @ValidEmail Annotation
- Create Constraint Class
- Create Data Model Class
- Create Web Controller
- Run Spring Boot Application & Enjoy Result
III. Practice
1. Create Spring Boot project & add Dependencies
Open
Spring Tool Suite, on
Menu, choose
File ->
New -> Spring Starter Project, then fill each fields. Remember that we will the
Packaging is
War.
Click
Next, then click
Finish.
Open pom.xml and add Dependencies:
https://grokonez.com/spring-framework/create-custom-validation-spring
How to create Custom Validation in Spring