Pages

Thursday, July 05, 2012

PrimeFaces AJAX Enabled <p:selectOneMenu />

I saw a question posed on stackoverflow called Trouble with Primefaces 3.0.M2 SelectOneMenu Ajax behavior and I had just done an example a couple of months ago on a car trip so I decided to post my solution here, and reference it.

Essentially the problem is how to control the list of items in a second <p:selectOneMenu/< from the selection made in the first menu. The question comes up frequently enough that I decided to write up a demo application.

The application was developed using NetBeans 7.1.2 and PrimeFaces 3.2 originally. It has been updated to use PrimeFaces 3.3.1 which is the most current at the time of writing. I validated it on GlassFish 3.1.2.

 The Apache Maven project can be downloaded from here: primefaces-ajax-selectone.zip

This demo wants the user to pick a state, and then a city. A key problem is not just the update, but not displaying city values if a state is not selected. Also erasing the selected city, if the state has changed.

index.xhtml



DataBean.java


5 comments:

  1. Hello,
    I got:
    java.lang.NullPointerException
    at com.bluelotussoftware.example.jsf.DataBean.stateChangeListener(DataBean.java:159)

    I run on glassfish 3.1.2 Do you know maybe reason?
    Thank you

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Interestingly... the exception is being caused by the valueChangeListener="#{dataBean.stateChangeListener(event)}". If you remove the (event), it will start working.

    ReplyDelete
  4. thank you soo much man your code helped me a lot _/\_

    ReplyDelete