af:selectonechoice not getting updated


Today i had an interesting problem at work. The issue is that the user selects the value from a selectonechoice and moves to the next page and when he returns back to the same old page the selectonechoice is not updated with the value that is selected previously.

the selectonechoice is not bounded to any of the bindings and we manually create the list using the managed bean. here is the base snippet which resembles the problem


selectOneChoice simple="true" value="#{Bean.list.itemNo}" id="soc1" unselectedLabel="Please Select">
 selectItems value="#{Bean.listItems}"/>
 </af:selectOneChoice>

while debugging the value the value is all available but somehow the value is not shown in the selectonechoice. The value is again moves to the “Please Select” option specified in the unselectedLabel when the user moves back to the original page.

couple of things i tried which failed
1)binding to the bean and getting the value

2) having the value as “#{Bean.list[‘itemNo’]}”

finally I got to see the following warning in the log saying

13-Jul-2011 20:27:54 oracle.adfinternal.view.faces.renderkit.core.xhtml.SimpleSelectOneRenderer _getSelectedIndex
WARNING: Could not find selected item matching value "1" in CoreSelectOneChoice[UIXEditableFacesBeanImpl, id=soc1]

This warning gave me a hint to think the problem in a different way and leads to the solution

Actually the list that we are manipulating assigns the value on the fly like


....

List<SelectItem> items = new Arraylist();

...

//inside the method

items.add(new SelectItem(code.getItemlabel(), code.getItemValue()));

....
 

the above code has to rewritten as shown to solve the above mentioned issue


//inside the method

SelectItem item = new SelectIem();

item.setLabel(code.getItemlabel());

item.setValue(code.getItemValue()); //here the value should be an Integer value
//if the value is a String then this has to be like
//item.setValue(Integer.parseInt(code.getItemValue()));
items.add(item);

....
 
Advertisements

8 thoughts on “af:selectonechoice not getting updated

  1. subu

    i had a same problem

    //inside the method
    02
    03 SelectItem item = new SelectIem();
    04
    05 item.setLabel(code.getItemlabel());
    06
    07 item.setValue(code.getItemValue()); //here the value should be an Integer value
    08 //if the value is a String then this has to be like
    09 //item.setValue(Integer.parseInt(code.getItemValue()));
    10 items.add(item);
    11
    12 ….

    —————————————————————————

    code in my bean: for select one choice n select item.
    public void setSoc6(RichSelectOneChoice soc6) {
    this.soc6 = soc6;
    }

    public RichSelectOneChoice getSoc6() {
    return soc6;
    }

    public void setSi10(RichSelectItem si10) {
    this.si10 = si10;
    }

    public RichSelectItem getSi10() {
    return si10;
    }

    //inside the method……… under which method? .

    item.setLabel(*code*.getItemlabel()); i does not have the set label ,get label .. what is *code.?

    item.setValue(code.getItemValue()); i does not have the set value ,get value

    items.add(item); what is items?

    am working in basic level. so i cant understud ur codes. so only am asking

    Reply
  2. subu

    u said..
    //inside the method

    SelectItem item = new SelectIem();

    item.setLabel(code.getItemlabel());

    item.setValue(code.getItemValue()); //here the value should be an Integer value
    //if the value is a String then this has to be like
    //item.setValue(Integer.parseInt(code.getItemValue()));
    items.add(item);

    i had some doubts:

    code in my bean: for select one choice n select item.
    public void setSoc6(RichSelectOneChoice soc6) {
    this.soc6 = soc6;
    }

    public RichSelectOneChoice getSoc6() {
    return soc6;
    }

    public void setSi10(RichSelectItem si10) {
    this.si10 = si10;
    }

    public RichSelectItem getSi10() {
    return si10;
    }

    please forgive me for asking this kind of doubts…

    //inside the method……… under which method? .

    item.setLabel(*code*.getItemlabel()); i does not have the set label ,get label . what is code.?

    item.setValue(code.getItemValue()); i does not have the set value ,get value

    items.add(item); what is items?

    am working in basic level. so i cant understud ur codes. so only am asking

    Reply
  3. Kavin

    I think the reason why your initial code dint work could be because of mismatch in the parameters that you are passing while initializing SelectItem.

    http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/javax/faces/model/SelectItem.html

    While initializing SelectItem, its,
    SelectItem(java.lang.Object value, java.lang.String label)

    But you seem to be passing the other way.
    new SelectItem(code.getItemlabel(), code.getItemValue())

    So did you try to sawp this and give a try?

    Reply
    1. Anonymous

      Actually it was a typo, I tried the same then and it was not working.. I found later to add a valuepassthru=true to enable the actual value that is bounded to get passed..

      Reply
  4. Peter

    Thanks
    Is there any way to exclude the ‘unselectedLabel’ from the list?
    In your case, we don’t want to give please select as a choice.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s