0

I am working with JSF 1.2 and could use a little advice. I have an FMB (PlanFMB.java) that contains an array list of select items and a single selectedItem.

When the page loads, the getProjects() method is correctly called and displays the Projects as expected. Strange thing is though, the getSelectedItem() method is called three times (once of each project). Not sure if this is typical behavior:

SystemOut     O getSelectedItem = null
SystemOut     O getSelectedItem = null
SystemOut     O getSelectedItem = null

Also, I have a command link in my JSP, that when clicked does the same thing, calls the getSelectedItem() method three times:

<h:commandLink action="#{planDocBean.classAction}" id="classActionENLink">

PlanFMB.java

String selectedItem = null;

private List<SelectItem> selectItems = null;

public String getSelectedItem() {
      System.out.println("getSelectedItem = " + selectedItem);
      return selectedItem;
}

public void setSelectedItem(String selectedItem) {
    this.selectedItem = selectedItem;
    System.out.println("setSelectedItem = " + selectedItem);
}

public List<SelectItem> getProjects() {       
  if (selectItems == null) {
    selectItems = new ArrayList<SelectItem>();
    selectItems.add(new SelectItem("Project1", "Project1"));
    selectItems.add(new SelectItem("Project2", "Project2"));
    selectItems.add(new SelectItem("Project3", "Project3"));
  }
  return selectItems;
}

<h:selectOneMenu id="items" value="#{planDocBean.selectedItem}"> 
<f:selectItems value="#{planDocBean.projects}" />
</h:selectOneMenu>  

One last thing, the actual selected item is not retained in the select menu when the page reloads.

Any feedback much appreciated. Thanks!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ChrisF
  • 25
  • 7
  • I don't see any `return` statement in `getProjects`, must be a typo. Can you please update your code. – Andy Jul 11 '13 at 03:35
  • Possible response to your question: http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times – LaurentG Jul 11 '13 at 03:41
  • Also, what do you mean by not retained. If you select say Project 2 and then submit the form, do you see Project 2 (or any previous selection) when the form reloads ? Are you basing this based on `getSelectedItem = null` when you submit your form ? – Andy Jul 11 '13 at 04:32
  • Fixed the typo in the question. Regarding the selected item (i.e Project 2) value being retained, when the user clicks on the action link and the form reloads, the selected item is lost and it defaults to "Project 1", the first value in the array list. Stepping through the code in debug, the setSelectedItem method is called, thus setting selectedItem as "Project 2", however, as I step through the code, it looks like the FMB constructor (and my init method) is getting called a couple of times and selectedItem is being set to null. My getProjects() method is also called a couple of times. – ChrisF Jul 11 '13 at 15:14
  • You shouldn't care about getter methods being called multiple times. EL calls the getter method on every evaluation. In this particular case, just to compare the submitted value against every item of the list in order to check if it's indeed one of the valid values (otherwise you'll get a validation error: value is not valid). What exactly is your concrete problem? You said something about being set to null? Please focus on that instead of on getters. Just do the business job in (post)constructor method instead and keep the getter a real getter. It should merely return already-prepared data. – BalusC Jul 11 '13 at 15:26

0 Answers0