gpt4 book ai didi

java - Jsf 2.0 填充 h :selectOneMenu to access page

转载 作者:行者123 更新时间:2023-11-29 06:19:36 25 4
gpt4 key购买 nike

我是 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");
}
}
  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;
}
}

最佳答案

          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>

关于java - Jsf 2.0 填充 h :selectOneMenu to access page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3672335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com