2010-09-08 34 views
1

我是JSF 2.0中的新手,我在JSF 1.1和1.2中工作,並且在Managed bean頁面的構造函數中填充selectOneMenu。用於訪問用戶訪問頁面列表時填充。下面的例子。我把它放在JSF 2.0中,但不起作用,selectOneMenu顯示爲空。Jsf 2.0填充h:selectOneMenu訪問頁面

<h:selectOneMenu id="cboStatus" value="#{PersonBean.idStatus}"> 
    <f:selectItems value="#{PersonBean.status}"/> 
</h:selectOneMenu> 

在構造函數中的託管bean我把:

public class PersonBean {  
    private SelectItem[] status=new SelectItem[0]; 

    public PersonBean() { 
     detallePersonas= new ArrayList(); 

status= new SelectItem[3]; 
status[0]=new SelectItem("S","Single"); 
status[1]=new SelectItem("M","Married"); 
status[2]=new SelectItem("D","Divorced"); 
    } 
} 
  1. 主編Netbeans的6.8(JSF 2.0缺省配置嚮導)
  2. 沒有異常錯誤
  3. 不要運行構造PersonBean(我把一個斷點,永不停止)
  4. 還有其他方法來填充選擇來加載頁面

這是完整的代碼:

的index.xhtml

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:ui="http://java.sun.com/jsf/facelets"> 
    <h:head> 
     <title>Person</title> 
    </h:head> 
    <h:body> 
     <h:form id="frmPerson"> 
      <h:outputLabel id="lblStatus" value="Status:"/> 
      <h:selectOneMenu id="cboStatus" value="#{PersonBean.idStatus}"> 
       <f:selectItems value="#{PersonBean.status}"/> 
      </h:selectOneMenu> 
     </h:form> 
    </h:body> 
</html> 

PersonBean.java

package com.prueba.backingbean; 

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 
import javax.faces.model.SelectItem; 

/** 
* 
* @author Administrador 
*/ 
@ManagedBean(name = "Person") 
@ViewScoped 
public class PersonBean { 

    private String idStatus; 
    private SelectItem[] status = new SelectItem[0]; 

    public PersonBean() { 
     status = new SelectItem[3]; 
     status[0] = new SelectItem("S", "Single"); 
     status[1] = new SelectItem("M", "Married"); 
     status[2] = new SelectItem("D", "Divorced"); 
    } 

    /** 
    * @return the idStatus 
    */ 
    public String getIdStatus() { 
     return idStatus; 
    } 

    /** 
    * @param idStatus the idStatus to set 
    */ 
    public void setIdStatus(String idStatus) { 
     this.idStatus = idStatus; 
    } 

    /** 
    * @return the status 
    */ 
    public SelectItem[] getStatus() { 
     return status; 
    } 

    /** 
    * @param status the status to set 
    */ 
    public void setStatus(SelectItem[] status) { 
     this.status = status; 
    } 
} 

回答

0

看吧:

@ManagedBean(name = "Person") 
@ViewScoped 
public class PersonBean { 

你聲明的託管bean名稱Person。所以它在JSF EL中可用,如#{Person}。但是,您試圖以#{PersonBean}的身份訪問它。由於這種bean不存在,菜單保持空白。

你已經有3個選項:

  1. 在JSF頁面中通過重命名#{Person}#{PersonBean}。在您的託管bean中將@ManagedBean(name = "Person")重命名爲@ManagedBean(name = "PersonBean")

  2. 擺脫bean名稱並使用默認的JSF命名約定。即只需使用@ManagedBean而不使用name,並在您的JSF頁面中使用#{personBean}(本質上,該bean的類名帶有小寫的第一個字符)。

選項3是優選的。

+0

好的正在工作。謝謝你們。特別是對BalusC。 – NestorInc 2010-09-11 20:44:01

+0

不客氣。 – BalusC 2010-09-11 21:01:42

1

它應該是 「personBean」,而不是「PersonBean 「(第一個字母應該是小寫)。您還需要getter的狀態(getStatus())和idStatus的設置者/獲取者(setIdStatus()/getIdStatus())。他們在嗎?

0

您是否嘗試過使用List而不是SelectItems數組。以下代碼可能會對您有所幫助。

private List<SelectItem> status = new ArrayList<SelectItem>(); 

status.add(new SelectItem("S","Single")); 
status.add(new SelectItem("M","Married")); 
status.add(new SelectItem("D","Divorced")); 

/* 
    SETTER/GETTER 
*/ 
+0

不,這兩種方式都同樣有效。 – BalusC 2010-09-09 14:05:11

+0

這是完整的代碼。請幫助我 – NestorInc 2010-09-10 01:11:22

2
  private Map<String,String>status = new HashMap<String,String>(); 
      ....... 
      status.put("S", "Single"); 
      status.put("M", "Married"); 
      status.put("D", "Divorced"); 

在JSF頁面:

  <h:selectOneMenu value="#{personBean.idStatus}" > 

      <f:selectItems value="#{personBean.status}"/>     

     </h:selectOneMenu>