Question

I would like to populate a <p:selectOneMenu> with String values from the database. So far, this is what I got (sorry about the Portugueese code):

<p:selectOneMenu value="#{onibusMB.nomeTransportadora}">
    <f:selectItem itemLabel="" itemValue="" />
    <f:selectItems value="#{onibusMB.transportadoras}" var="transportadora" itemValue="#{transportadora}"/>
</p:selectOneMenu>

Where my bean is:

@Named
@RequestScoped
public class OnibusMB implements Serializable {

    private List<Transportadora> transportadoras;
    private String nomeTransportadora;
    /*getter, setter, etc

}

And my entity is:

@Entity
@Table(name = "TRANSPORTADORA")
public class Transportadora implements AbstractEntity {

    private String nome;

}

I tried a few changes in that, but none has worked. The label shows always:

com.ezpass.model.Transportadora@2ce4a0c2

How can I show the nome property of Transportdora in label?

Was it helpful?

Solution

You forgot to set the itemLabel attribute.

<f:selectItems ... itemLabel="#{transportadora.nome}" />

The itemValue only sets the actual item value, not the item label. The item value is what's been submitted from client to server. The item label is what's been shown to the world. If the item label is absent, then by default the item value is shown. But as it's a complex entity and you didn't specify any converter, then it defaults to toString() result of Transportadora class.

See also:


Unrelated to the concrete problem, you perhaps also want to set itemValue to the same as in

<f:selectItems ... itemValue="#{transportadora.nome}" />

because your property is a String, not a Transportdora. You (read: JSF) can't set a Transportdora object in a String property. Otherwise, you need to change it to #{onibusMB.transportadora} and provide a converter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top