0

JSF – Basic Form Validation for Required Fields

This post demonstrates how to perform validation on required fields in JSF.

Requirements

Stuff used in this post.

  • JSF 2.2
    • javax.faces-2.2.8.jar
  • JDK 8

JSF Managed Bean

This is our managed bean is a Java class annotated with @ManagedBean. The 3 important piece of codes here are:

  1. @ManagedBean
  2. Default public no-arg constructor
  3. Setter and getter methods

For this post, the following is our Java class.

Registration Page – Request

The first page is where users input employee information. Hit Submit button to validate data and finally register an employee.

Second Page – Response

This page displays the registered employee details

Testing

With Validation Errors

The required fields are Last Name and Email. In order to test with validation error, we’ll submit the page with only First Name provided.

Request Page

Response

The page is unable to proceed and the user sees the errors.

Without Validation Errors

Request Page

Response

The user is then shown details of registered employee.

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert