JSF selectOneMenu selectItem始终为null

时间:2010-01-26 08:51:57

标签: jsf selectonemenu

我正在尝试使用支持bean实现JSF selectOneMenu项   选择。问题是selectedItem始终为null。这里   是代码:

.xhtml:

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

豆:

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>

我正在使用sun实现(版本1.2)。问题出现在Jetty 6上 和WebSphere 6.1。

有人可以帮我解决这个问题吗?

基督教

1 个答案:

答案 0 :(得分:2)

我忘记了一些事情:我添加了<h:form>,我必须执行以下操作:

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

我不明白的是,在网上的例子中,没有人这样做。