newbie: h:selectOneMenu performance problem
mars1412 Mar 14, 2008 1:34 PMI am currently struggling with an issue, I don't understand.
when I use a selectOneMenu in my page and use the value attribute, it seems like the testBean is created once for each element in the selectItems!
I hope that I am doing something wrong and that this is not the expected behavior.
I'll post my code and some comments.
This is the code in my page
<h:selectOneMenu value="#{testBean.selectedItem}"> <f:selectItems value="#{testBean.items}"/> </h:selectOneMenu>
my testbean is simple:
@Name("testBean") @Scope(ScopeType.CONVERSATION) @Install(debug = true) public class TestBean implements Serializable { /** The Constant serialVersionUID. */ private static final long serialVersionUID = 1L; private static final int NO_OF_SELECT_ITEMS = 200; @Logger Log log; Integer selectedItem = 7; public Integer getSelectedItem() { System.out.println("getSelectItem called"); return selectedItem; } public void setSelectedItem(Integer selectedItem) { System.out.println("setSelectItem called"); this.selectedItem = selectedItem; } public List<SelectItem> getItems() { System.out.println("get Items called"); List<SelectItem> selectItems = new ArrayList<SelectItem>(NO_OF_SELECT_ITEMS); for (int i = 0; i < NO_OF_SELECT_ITEMS; i++) { selectItems.add(new SelectItem(i, Integer.toString(i) + "xxxx")); } return selectItems; } }
when I access the page, the output is like this:
13:24:10,950 INFO [DebugObserver] JSF phase: before RENDER_RESPONSE 6 13:24:10,966 INFO [STDOUT] get Items called 13:24:10,966 INFO [STDOUT] get Items called 13:24:10,966 INFO [STDOUT] getSelectItem called 13:24:10,966 INFO [STDOUT] getSelectItem called +++++ 198 TIMES !! 13:24:11,419 INFO [DebugObserver] JSF phase: after RENDER_RESPONSE 6
- why is getSelectItem called 198 times in the render response phase?
- well calling the simple getter is not the problem, the real problem is, that a testBean instance will be created for each of these calls: from the JBoss server.log, I can see, that between the before and after RENDER_RESPONSE phase, I find the following line for 200 times:
2008-03-14 13:16:37,555 DEBUG [org.jboss.seam.contexts.Contexts] found in conversation context: testBean
when I omitt the value attribute:
<h:selectOneMenu> <f:selectItems value="#{testBean.items}"/> </h:selectOneMenu>
I get this output.
13:30:04,811 INFO [DebugObserver] JSF phase: before RENDER_RESPONSE 6 13:30:04,811 INFO [STDOUT] get Items called 13:30:04,811 INFO [STDOUT] get Items called 13:30:04,826 INFO [DebugObserver] JSF phase: after RENDER_RESPONSE 6
even in this case I don't understand why getItems() is called twice ...