aspose file tools*
The moose likes JSF and the fly likes JSF lifecycle – selectonemenu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF lifecycle – selectonemenu" Watch "JSF lifecycle – selectonemenu" New topic
Author

JSF lifecycle – selectonemenu

Men rai
Greenhorn

Joined: May 31, 2011
Posts: 11
Hi,
I have a behaviour that I don’t understand with a selectonemenu. I will try to explain it wondering if somebody know what’s happening. I am using JSF2 with glassfish3.
My xhtml code is as follows:

<h:selectOneMenu binding="#{cetBacking1.menuFamilies}"
valueChangeListener="#{cetBacking1.listenerFamilia}"
onchange="this.form.submit()">
<f:selectItems value="#{cetBacking1.ompleFamiliesTemplate()}"></f:selectItems>
</h:selectOneMenu>
<h:commandButton value="Test" type="submit" action="#{cetBacking1.test()}"></h:commandButton>

I use binding because depending on the selectOneMenu value I will render some other components but now this is out of scope. The command button is for the submit. ompleFamiliesTemplate() returns a list of selectitems.
And the backing bean is:

@ManagedBean(name="cetBacking1")
@SessionScoped
public class cetBacking1 extends AbstractEntityAccessor implements Serializable {
private List<SelectItem> llistaFamilies = new ArrayList<SelectItem>();
private HtmlSelectOneMenu menuFamilies;
{
System.out.println("cetBacking1.init");
ompleFamiliesTemplate();
}

public void listenerFamilia(ValueChangeEvent event) {
String novaFamilia = (String) event.getNewValue();
System.out.println("cetBacking1.listenerFamilia: " + novaFamilia);
}

public List ompleFamiliesTemplate() {
FacesContext context = FacesContext.getCurrentInstance();
List<FamiliaTemplate> llistaFamiliestmp = new ArrayList<FamiliaTemplate>();
System.out.println("cetBacking1.ompleFamiliesTemplate !!!");
if (context.getExternalContext().getSessionMap().containsKey("LlistaFamilies")) {
llistaFamiliestmp = (List<FamiliaTemplate>) context.getExternalContext().getSessionMap().get("LlistaFamilies");
if (llistaFamilies.isEmpty()) {
for (Iterator it = llistaFamiliestmp.iterator(); it.hasNext();) {
FamiliaTemplate object = (FamiliaTemplate) it.next();
llistaFamilies.add(new SelectItem(String.valueOf(object.getIdfamiliaTemplate()), object.getFamilia()));
}
}
}
return llistaFamilies;
}
public void submit() {
System.out.println("cetBacking1.submit !!");
}

I put some System.out.println to follow the calls to the different functions, the cetBacking1 bean constructor and ompleFamiliesTemplate(), and the valueChangeListener.
The first time the page is loaded in the server log appears the next messages:
INFO: cetBacking1.init
INFO: cetBacking1.ompleFamiliesTemplate !!!
INFO: cetBacking1.ompleFamiliesTemplate !!!
The {} function of the bean is called, it calls cetBacking1.ompleFamiliesTemplate and the second cetBacking1.ompleFamiliesTemplate is called by the f:selectItems. It works as expected. Now I change tha value of the selectOneMenu and in the log appears:
INFO: cetBacking1.ompleFamiliesTemplate !!!
INFO: cetBacking1.ompleFamiliesTemplate !!!
INFO: cetBacking1.listenerFamilia: 2
INFO: cetBacking1.listenerFamilia: 2
INFO: cetBacking1.ompleFamiliesTemplate !!!
The last call to cetBacking1.ompleFamiliesTemplate I think that is called by f:selectItems. But I do not why is called twice at the beginning cetBacking1.ompleFamiliesTemplate and then twice the listener cetBacking1.listenerFamilia.
But the next is more amazing for me. I change again the selectOneMenu and in the log appear:
INFO: cetBacking1.ompleFamiliesTemplate !!!
INFO: cetBacking1.ompleFamiliesTemplate !!!
INFO: cetBacking1.listenerFamilia: 3
INFO: cetBacking1.listenerFamilia: 3
INFO: cetBacking1.listenerFamilia: 3
INFO: cetBacking1.ompleFamiliesTemplate !!!
The cetBacking1.ompleFamiliesTemplate is called twice at the beginning as before but the listener is called three times. Each time I change the selectOneMenu the listener is called one more time. If I change the selectOneMenu value two times the log is as follows:
INFO: cetBacking1.ompleFamiliesTemplate !!!
INFO: cetBacking1.ompleFamiliesTemplate !!!
INFO: cetBacking1.listenerFamilia: 1
INFO: cetBacking1.listenerFamilia: 1
INFO: cetBacking1.listenerFamilia: 1
INFO: cetBacking1.listenerFamilia: 1
INFO: cetBacking1.ompleFamiliesTemplate !!!
INFO: cetBacking1.ompleFamiliesTemplate !!!
INFO: cetBacking1.ompleFamiliesTemplate !!!
INFO: cetBacking1.listenerFamilia: 2
INFO: cetBacking1.listenerFamilia: 2
INFO: cetBacking1.listenerFamilia: 2
INFO: cetBacking1.listenerFamilia: 2
INFO: cetBacking1.listenerFamilia: 2
INFO: cetBacking1.ompleFamiliesTemplate !!!
The question is why cetBacking1.ompleFamiliesTemplate is called twice when I change the selectOneMenu value, before the listener execution. And why the listener is executed twice the first time I change the selectOneMenu value, and then one more time every time I change the value.
Thanks in advance
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15624
    
  15

If you wrap formatted data (code, XML, and so forth) using the "code" tags (there's an editor button for that), it makes things a lot easier to read.

JSF references bean properties many times as it processes a page request. This is normal. It's also why "get" methods in JSF should not contain heavy logic or have side-effects.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JSF lifecycle – selectonemenu
 
Similar Threads
problem while setting default value for h:selectOneMenu
How to create an action listner?
selectOneMenu and value in the backing Bean in JSF
Issue With selectOneMenu ValueChangeListener Not Firing
How to set default selection for selectOneMenu?