tags:

views:

65

answers:

3

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

Thanks for helps


Thanks for your help

  1. Editor Netbeans 6.8 (JSF 2.0 default configuration wizard)
  2. No Exception Error
  3. Never run the constructor PersonBean (I put a breakpoint and never stops)
  4. There are other ways to populate selects to load the page

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"&gt;
<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"&gt;
    <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;
    }
}

Help me please

+1  A: 

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?

amorfis
A: 

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
*/
Nayan Wadekar
Nope, both ways are equally valid.
BalusC
This is the complete code. Please help me
NestorInc
A: 

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:

  1. Rename #{PersonBean} by #{Person} in your JSF page.

  2. Rename @ManagedBean(name = "Person") to @ManagedBean(name = "PersonBean") in your managed bean.

  3. 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.

BalusC
Ok is working right now. Thank you all. especially to BalusC.
NestorInc
You're welcome.
BalusC