Reputation: 13
I am newbie in JSF 2.0, I worked in JSF 1.1 and 1.2 and I populate selectOneMenu in constructor of Managed bean's page. For when users to acces to page the List is populate. example below. I put the same in JSF 2.0 but is not work, the selectOneMenu appears empty.
<h:selectOneMenu id="cboStatus" value="#{PersonBean.idStatus}">
<f:selectItems value="#{PersonBean.status}"/>
</h:selectOneMenu>
In constructor's managed bean I put:
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");
}
}
This is the complete code:
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;
}
}
Upvotes: 1
Views: 3318
Reputation: 21
private Map<String,String>status = new HashMap<String,String>();
.......
status.put("S", "Single");
status.put("M", "Married");
status.put("D", "Divorced");
in JSF PAGE:
<h:selectOneMenu value="#{personBean.idStatus}" >
<f:selectItems value="#{personBean.status}"/>
</h:selectOneMenu>
Upvotes: 2
Reputation: 1108642
Look here:
@ManagedBean(name = "Person")
@ViewScoped
public class PersonBean {
You've declared the managed bean name as Person
. So it's in JSF EL available as #{Person}
. Yet you're attempting to access it as #{PersonBean}
. Because such a bean does not exist, the menu remains empty.
You've 3 options:
Rename #{PersonBean}
by #{Person}
in your JSF page.
Rename @ManagedBean(name = "Person")
to @ManagedBean(name = "PersonBean")
in your managed bean.
Get rid of bean name and use the default JSF naming conventions. I.e. just use @ManagedBean
without a name
and use #{personBean}
in your JSF page (in essence, the bean's class name with first character lowercased).
Option 3 is preferred.
Upvotes: 0
Reputation: 11602
Have you tried using List instead of Array of SelectItems. Below code might help you.
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
*/
Upvotes: 0
Reputation: 15770
It should be "personBean" instead of "PersonBean" (first letter should be lowercase). You also need getter for status (getStatus()
) and setter/getter for idStatus (setIdStatus()
/getIdStatus()
). Are they there?
Upvotes: 1