0

Hello I have a component selectOneMenu from JSF Bootsfaces and I would like to fill it with values from backing bean.

This is what I have tried and it hasn`t worked:

This is the xhtml:

<b:selectOneMenu ajax="true" process="@this" label="Selecteaza CNP sau CUI">         
  <f:selectItems value="#{cereri.cnpcui}" var="beer"
                 itemValue="1" itemLabel="#{cereri.cnpcui}" />
</b:selectOneMenu>

This is the java bean:

@ManagedBean(name = "cereri", eager = true)
@RequestScoped
public class Cereri {

private List<String> cnpcui;

public Cereri() {
    cnpcui = new ArrayList<>();
    cnpcui.add("CUI");
    cnpcui.add("CNP");
}

public List<String> getCnpcui() {

    return cnpcui;
}

public void setCnpcui(List<String> cnpcui) {
    this.cnpcui = cnpcui;
}

The dropdown displays both values on each row as an array: [CNP,CUI] on both rows. I would like one value on first row and second value on second row.

Thanks

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Doseda
  • 39
  • 1
  • 6
  • 2
    So if you replace the `b:selectOneMenu` with an `h:selectOneMenu` it works? – Kukeltje Feb 21 '20 at 09:48
  • @JasperdeVries: NP... ;-) was a small edit – Kukeltje Feb 21 '20 at 09:50
  • I replaced b: with h: and it still display all the array – Doseda Feb 21 '20 at 10:35
  • So your question is in no way Bootsfaces related... Did you check WHAT you assign in the label **of each record** Debug... set breakpoints... etc... Take a basic tutorial for JSF or look at a showcase or even the bootsfaces showcase, compare etc... That is what you should initially all do before posting on stackoverflow – Kukeltje Feb 21 '20 at 11:22

1 Answers1

3

You are not using the component correctly. When you reference itemLabel="#{cereri.cnpcui}" you are telling the component to output the whole array.

To get the behaviour you are after, you need to do something like this;

<b:selectOneMenu ajax="true" process="@this" label="Selecteaza CNP sau CUI">         
        <f:selectItems value="#{cereri.cnpcui}" var="beer" 
                       itemValue="#{beer}" itemLabel="#{beer}" />
</b:selectOneMenu>

Each item from the list referenced by the value attribute (a string in this case) is placed in the locally scoped variable beer. Referencing #{beer} in the expression will therefore instruct the component to output the actual string.

Adam Waldenberg
  • 2,271
  • 9
  • 26
  • Thank you very much it worked – Doseda Feb 24 '20 at 07:13
  • 1
    Glad it helped @Doseda. I also recommend you take a close look at https://stackoverflow.com/questions/6848970/how-to-populate-options-of-hselectonemenu-from-database as referenced above, as it shows you some recommended practices. For example, you should never use the constructor in a managed bean when you initialize the view data. Among other things, this is because of the way serialization and object reuse works. Instead, use a private init method annotated with the `@PostConstruct` annotation. – Adam Waldenberg Feb 24 '20 at 09:05