JSF - valueChangeListener example!

Lets see!

This is the Managed Bean, Sample.java
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 7/11/2016
 * Time: 7:49 PM
public class Sample {
    private String sampleValue;
    public String getSampleValue() {
        return sampleValue;
    public void setSampleValue(String sampleValue) {
        this.sampleValue = sampleValue;
    public void sampleAction() {
        System.out.println("Action called!");

And the view, sample.xhtml
<?xml version="1.0" encoding="UTF-8"?>
    User: Koray Tugay (koray@tugay.biz)
    Date: 7/11/2016
    Time: 7:49 PM
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    <title>Sample Page</title>
    <h:form id="sampleForm" prependId="false">
        <h:selectOneMenu id="sampleListBox"
            <f:selectItem itemLabel="Foo" itemValue="Foo"/>
            <f:selectItem itemLabel="Bar" itemValue="Bar"/>
            <f:selectItem itemLabel="Baz" itemValue="Baz"/>

I believe this is the smallest possible example that can show how a valueChangeListener works. I may well be wrong.

Everytime I pick a different value in the Select Box, I will see in the console:
Action called!

  • If you include a Submit Button in the form, it will not be sent to the server. Why? Here is why! So basically, the reason seems to be: Submit buttons are only considered successful controls if they are used to submit the form. Since you are submitting the form using JavaScript, you aren't using the submit button, so it isn't successful, and it isn't included in submitted request.
  • If you want to send the form together with the Submit Button, you will need something like this: document.getElementById('submitButton').click();
  • If the action attribute of the commandButton returns a String, JSF will do its magic, either redirecting or forwarding etc..