Validating Numbers
Explore how to validate numeric input in Spring MVC by adding a rank field with Integer type. Learn to use @Min, @Max, and @NotNull annotations to enforce value constraints and required input. Understand how Spring MVC handles type conversion errors and displays validation messages to guide user input effectively.
We'll cover the following...
In this lesson, we will show how to validate a number field. Currently, we don’t have a number field in the Athlete class. We will add a field rank to store the current ranking the player. The value of rank can be between 1 and 100. Any value outside this range should result in a validation error. The datatype of the newly added field is Integer instead of the primitive type int as it will help in making the field required and handling type conversion errors.
public class Athlete {//...private Integer rank;//...public Integer getRank() {return rank;}public void setRank(Integer rank) {this.rank = rank;}}
We have used the Integer wrapper class as it enhances the robustness of form validation and error handling in web ...