1

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Pvic
  • 101
  • 1
  • 10

1 Answers1

1

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • It worked fine just by setting the itemLabel atribute. Even with a ""nome" cannot be resolved" warning in it. – Pvic Sep 20 '13 at 14:57
  • 1
    You're welcome. That warning is because the average IDE pretends to be smarter than it actually is as to validating EL expressions. EL is so complex, no one IDE is completely flawless in this. Eclipse not, Netbeans not and even IntelliJ not. – BalusC Sep 20 '13 at 15:00
  • Hum...i see. I will pay atention to that. Usually with theese warningns i tend to believe something is wrong with the code. I use Eclipse. Thanks a lot, man. – Pvic Sep 20 '13 at 15:05
  • Indeed. Just follow the documentation and just run the code outright regardless of the EL warnings. If it works, then you may assume that it's valid. If necessary, disable EL validation altogether in IDE preferences. – BalusC Sep 20 '13 at 15:07