Custom Validation Annotation
Learn how to create an annotation to validate custom business rules.
In this lesson, we will create an annotation that implements some custom business rules. Spring will validate the user input against our custom validation rules and display our custom error message if the input is not in the desired format.
As an example, we will create a form to enter the record of matches between two players. The form will prompt the user to enter two players, along with the head-to-head match score. The head-to-head score must follow a specific format. Two numbers must be separated by a hyphen. For example, 28-10 is a valid input. Characters are not allowed for this field. We will attach our custom annotation to validate the head-to-head score against the above-mentioned requirements.
First, we will create a new class called PlayerStats
in io.datajek.springmvc.tennisplayerweb.formtags
package having three fields as follows:
public class PlayerStats {private String player1;private String player2;private String head2head;// Default Constructorpublic PlayerStats() {}// Getters and Setterspublic String getPlayer1() {return player1;}public void setPlayer1(String player1) {this.player1 = player1;}public String getPlayer2() {return player2;}public void setPlayer2(String player2) {this.player2 = player2;}public String getHead2head() {return head2head;}public void setHead2head(String head2head) {this.head2head = head2head;}}
Next, we will create a controller class StatsController
in the same package (io.datajek.springmvc.tennisplayerweb.formtags
) to handle requests to show and process the form.
@Controllerpublic class StatsController {// method to handle /showStatsForm@RequestMapping("/showStatsForm")public String showForm(Model model) {model.addAttribute("playerStats", new PlayerStats());return "head-to-head";}// method to handle /processStatsForm}
When a request is sent to http://localhost:8080/spring/showStatsForm
, it is mapped to the showForm
method of the StatsController
class. The client is directed to the head-to-head
form. An empty PlayerStats
object called playerStats
is added as a model attribute.
Now, we will create the JSP page, head-to-head
, in the WEB-INF/views
folder. The layout of the form is shown below:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %><!DOCTYPE html><html><head><title>Head-to-Head</title></head><body><h2>Head-to-Head Statistics</h2><hr><form:form action = "processStatsForm" modelAttribute="playerStats"><br><br>Player 1: <form:input path = "player1"/>   vs.  Player 2: <form:input path = "player2"/><br><br>Head-to-Head: <form:input path = "head2head" placeholder="##-##"/>  <br><br><input type ="submit" value = "Submit"/></form:form></body></html>
We have a taglib
reference at the top which is needed to use Spring MVC form tags. The form has three text fields for taking the player names and head-to-head score as input. The modelAttribute
playerStats
binds the PlayerStats
bean to the form. When "Submit" is clicked, a request is sent to processStatsForm
.
The processForm()
method in the StatsController
class has a request mapping of ...