views:

1291

answers:

1

Hi, I'm trying to implement a JSF selectOneMenu item with a backing bean holding the selection. The problem is that the selectedItem is always null. Here is the code:

.xhtml:

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

Bean:

public class ComponentFilterBean {

 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;
 }
}

faces-config.xml:

<managed-bean>
  <managed-bean-name>componentFilterBean</managed-bean-name>  
  <managed-bean-class>test.ComponentFilterBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

I am using the sun implementation (version 1.2). The problem appears on Jetty 6 and WebSphere 6.1.

Can anybody help me with that problem?

Christian

A: 

I forgot something: I added a <h:form> and I had to do the following:

<h:selectOneMenu
  value="#{componentFilterBean.selectedItem}"
  immediate="true" onchange="javascript: return this.form.submit();">
  <f:selectItems value="#{componentFilterBean.projects}" />
</h:selectOneMenu>

What I don't understand is that in the examples on the net, nobody does it that way.

Christian
..because it's too obvious. You can't submit a form without a form.
BalusC