Software Development

JSF – Basic Form Validation for Required Fields

[wp_ad_camp_5]

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.

[wp_ad_camp_4]

Registration Page – Request

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

[wp_ad_camp_3]

Second Page – Response

This page displays the registered employee details

[wp_ad_camp_2]

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.

[wp_ad_camp_1]

Without Validation Errors

Request Page

Response

The user is then shown details of registered employee.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like