Skip to main content

JSF Tutorial – Listeners and Standard Validators

A note: Starting from this tutorial, I will dive into the depth of each component included in the Java EE. The following series of blogs will be talking about JSF technology, from beginner to advanced level, and it still uses Notebook as the study project and demonstrate functionalities provided by JSF.

Now let’s talk about JSF listeners and standard validators. There are two types of listeners in JSF, action listener and value change listener.

The project source code can be found on Github


  • Action listeners are typically triggered by <h:commandButton> and <h:commandLink>.
  • Value change listener are triggered by those who can take user inputs, such as <h:inputText>, <h:selectOne>.

For example, let’s define an action listener in the notebook project. Open index.xhtml, add an actionListner attribute to the <h:commandLink> tag, the one with id listUsers, like below:

<p><h:commandLink id="listUsers" action="#{userBackBean.getAllUsers}"
                actionListener="#{userBackBean.increaseClickCounts}">List All Users</h:commandLink></p>

The #{userBackBean.increaseClickCounts} invokes a bean method that is defined in the userBackBean as following code:

public void increaseClickCounts(ActionEvent event) {
    FacesContext context = FacesContext.getCurrentInstance();
    Object obj = context.getExternalContext().getSessionMap().get("count");
    if(obj != null) {
        Integer count = (Integer) obj;
        context.getExternalContext().getSessionMap().put("count", count);
    } else {
        context.getExternalContext().getSessionMap().put("count", 1);


It first retrieves the count value from the session, using “count” key. The session is actually a map provided by JSF context. You can think getCurrentInstance() as the page triggered the event. If the count value is not null, increase it by 1, otherwise the value will be initiated to 1. Then, in the user_list.xhtml page, add a <h3> tag to display the count value:

<h3>View counts: #{count}</h3>

After click sever times of List All Users link in the index.xhtml page, you will see the count value is increasing in the user_list.xhtml page:

For the value change listener, this may be more useful in Ajax mode, but I can still give you an illustrative way on how to use it. Open register.xhtml page, we can register a value change listener to the username input text by adding a valueChangeListener, and the value is the bean method that this listener is referencing:

<h:inputText id="usernmae" value="#{userBackBean.user.username}"
                     valueChangeListener="#{userBackBean.userNameChanged}" >

In the UserBackBean class, define the following method:

public void userNameChanged(ValueChangeEvent event) {
    if(null != event.getNewValue()) {"Entering user name changed listener: " + event.getNewValue());


It just output the log which displays the new value that the user input:

2017-06-02 20:03:49,576 INFO  [com.zxuqian.notebook.web.UserBackBean] (default task-12) Entering user name changed listener: null -> yyyy

Standard validators

This time I will talk about JSF standard validators. JSF provides more powerful mechanisms to support custom validators and I will discuss this in future blogs. Standard validators are (from Java EE tutorial):

Validator Class Tag Function
BeanValidator validateBean Registers a bean validator for the component.
DoubleRangeValidator validateDoubleRange Checks whether the local value of a component is within a certain range. The value must be floating-point or convertible to floating-point.
LengthValidator validateLength Checks whether the length of a component’s local value is within a certain range. The value must be a java.lang.String.
LongRangeValidator validateLongRange Checks whether the local value of a component is within a certain range. The value must be any numeric type or String that can be converted to a long.
RegexValidator validateRegex Checks whether the local value of a component is a match against a regular expression from the java.util.regex package.
RequiredValidator validateRequired Ensures that the local value is not empty on an EditableValueHoldercomponent.

As the RegexValidator is the most flexible validator, so I will take this as an example to illustrate how to use these validators. Open the register.xthml, we will add a regular expression to limit the length and letters that can be used as username, and a message tag to display the message if the value failed the validator:

<h:inputText id="username" value="#{userBackBean.user.username}"
             valueChangeListener="#{userBackBean.userNameChanged}" >
    <f:validateRegex pattern="[a-zA-Z0-9_]{6,}"/>
<h:message for="usernmae" />

If you enter the username with less than 6 characters or not in the a-zA-Z0-9_ range, a standard failed message will be shown, you can customize the message using custom validators:

Now, you can see how the listeners and validators work. I will introduce more advanced and practical usages in the future. Should you have any question, please feel free to contact me.