【发布时间】:2010-09-08 22:19:40
【问题描述】:
我是 JSF 2.0 的新手,我曾在 JSF 1.1 和 1.2 中工作,我在托管 bean 页面的构造函数中填充 selectOneMenu。当用户访问页面时,会填充列表。下面的例子。我在 JSF 2.0 中放了同样的东西但不起作用,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");
}
}
- 编辑器 Netbeans 6.8(JSF 2.0 默认配置向导)
- 无异常错误
- 从不运行构造函数 PersonBean(我放了断点,从不停止)
- 还有其他方法可以填充选择以加载页面
这是完整的代码:
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;
}
}
【问题讨论】: