Validating Required Fields

Learn how to validate required fields in a Spring MVC application.

A required filed is a mandatory field that must be filled in before the form is submitted. In this lesson, we will add a required field to our form. The add-player-form has a textbox for name that stores data in the lastName field of the Athlete class. We will change this field and split it into first name and last name, with the latter being a required field. The first name can be left blank.

When the user clicks on the "Add player" button on the add-player-form.jsp , the request goes to the AthleteController class which performs validation check and based on the results, either sends the user to the player-confirmation.jsp page or back to the add-player-form.jsp for resubmission.

First, we will add the field firstName to the Athlete class along with its getter and setter methods as follows:

Get hands-on with 1400+ tech skills courses.