0

I need some help. I'm developing for jsf and primefaces web application and I'm facing a problem when I'm selecting from a drop down list to get the selected value but I'm getting an empty string in the action.

This is my xhtml code for selectOneMenu tag

<p:selectOneMenu value="#{tanAllot.batchName}" id="batchName">
    <f:selectItem itemLabel="Select Batch" itemValue="" />
    <f:selectItems value="#{tanAllot.batchList}" />
    <p:ajax event="change" listener="#{tanAllot.test}" />
</p:selectOneMenu>

this is the method I'm using in the action class

private String batchName;

public String getBatchName() {
    return batchName;
}

public void setBatchName(String batchName) {
    this.batchName = batchName;
}

public void test() {
    System.out.println(batchName);
}

My problem is when I select a value from p:selectOneMenu tag the default method should invoke in the action and retrieve the value but I'm getting an empty string.

Can anyone help me to solve this problem?

Andy
  • 5,900
  • 2
  • 20
  • 29
user2544942
  • 11
  • 1
  • 5

1 Answers1

0

Consider the nature of batchList. batchList must be List of Strings. Using itemValue attribute (in f:selectItem) can be helpful.

Check my example. It uses a list of provinces (instances of "Province" class). However I only need the "id" value which is a "Long"; if I wanted the whole picked province as a "Province object" I would need a "Converter". (Example works perfectly):

<p:selectOneMenu id="provinceField" 
        value="#{addAddressesMB.formAddress.provinceId}">
    <f:selectItem itemLabel="Select One" itemValue="" />
    <f:selectItems value="#{addAddressesMB.provinceList}" var="i"
        itemLabel="#{i.description}" itemValue="#{i.id}" />
        <p:ajax update=":formId:cityField"
            listener="#{addAddressesMB.provinceChangeHandler}" />
</p:selectOneMenu>

And here comes the listener method:

public void provinceChangeHandler() {
    //do whatever you want with formAddress.provinceId
    System.out.println(this.formAddress.provinceId);
    //In my case I filter the cities according to the selected provinceId
    // After that I update the cities dropdown(cityField) in the view.
}

Check your code and feel free to ask. Good luck.

theCowboy
  • 186
  • 2
  • 6