0

I want to show in a list the names of the buildings have stored in the database, using h: selectOneMenu f: SelectItems. Part of .xhtml where I do this is:

    <h:form id="form-lista">
    <h:selectOneMenu value="#{controller.edificioSeleccionado}">
        <f:selectItems value="#{controller.edificios}" />
    </h:selectOneMenu>
</h:form>

The Bean controller has the following code:

@ManagedBean
public class BeanEdificios implements Serializable {
private static final long serialVersionUID = 55555L;


private Edificio[] edificios =  null;
private String edificioSeleccionado = "";


//private Edificio EdificioSeleccionado = null;




public Edificio[] getEdificios () {

    listado();

    return(edificios);
}

public String getEdificioSeleccionado() {
    return edificioSeleccionado;
}

public void setEdificioSeleccionado(String edificioSeleccionado) {
    this.edificioSeleccionado = edificioSeleccionado;
}

public String listado() {

    EdificioJdbcDao jdbc = new EdificioJdbcDao();
    try {
        // De esta forma le damos informaciónn a toArray para poder hacer el casting a Edificio[]
        edificios = (Edificio [])jdbc.getEdificios().toArray(new Edificio[0]);


        return "exito";

    } catch (Exception e) {
        e.printStackTrace();  
        return "error";
    }

}

And the kind of model buildings have these fields:

public class Edificio {

private int id_edificio;
private String nombre;
private boolean reservado;



public Edificio(int id_edificio, String nombre, boolean reservado) {
    this.id_edificio = id_edificio;
    this.nombre = nombre;
    this.reservado = reservado;
}
public Edificio() {
}
public int getIdEdificio() {
    return id_edificio;
}
public void setIdEdificio(int id_edificio) {
    this.id_edificio = id_edificio;
}

public String getNombre() {
    return nombre;
}
public void setNombre(String nombre) {
    this.nombre = nombre;
}
public boolean isReservado() {
    return reservado;
}
public void setReservado(boolean reservado) {
    this.reservado = reservado;
}

}

By doing this, the list makes me the name of the record in the database, something I do not want:

Edificio@78bb1ce9 Edificio@52cd1c5e Edificio@32ac1d32

I want to show the name field (or id) of the building. I thought about putting in the value attribute of f: SelectItems something like this, but I get error. How could I?

<f:selectItems value="#{controller.edificios.nombre}" />
gabi13
  • 101
  • 8

1 Answers1

0

Use

private SelectItem[] edificios =  null;

instead of

private Edificio[] edificios =  null;

and inside SelectItem you can put whatever you want like this:

new SelectItem ("id","nombre");

first param is item value, and second is item label.

Safwan Hijazi
  • 2,089
  • 2
  • 17
  • 29