0

I wanted to create in JSF a combobox (selectOneMenu). I want to fill this combobox with all logins from one column from Database (SELECT logins FROM database).

Will be much gratefull from any help.

Zyngi
  • 31
  • 1
  • 7

1 Answers1

0

In your backing bean (YourBean in the example) you should have an array of Strings (or of objects with a getter method that returns the String you want). For instance lets assume you have the following code in your backing bean:

private ArrayList<String> logins; // read from DB
private String selectedLogin; // this will hold the selected value

// this method will be called by the JSF framework to get the list
public ArrayList<String> getLogins()
{
   return logins;
}

public String getSelectedLogin()
{
    return selectedLogin;
}


public String setSelectedLogin(String sl)
{
    selectedLogin = sl;
}

In you Facelets page, assuming you are on JSF 2.x:

<h:selectOneMenu value="#{YourBean.selectedLogin}">
            <f:selectItems value="#{YourBean.logins}" itemLabel="#{l}" itemValue="#{l}" var="l"/>
 </h:selectOneMenu>

This will create a select menu with all the options in the array. Once the form is submitted the value will be set in the String value of your backing bean.

elbuild
  • 4,869
  • 4
  • 24
  • 31
  • I occured 1 little problem. When I use "itemValue="#{l}"" in code, page doesn't load to me, so I skipped it. But without it, beans cannot read selected value and always is null. Do you know what could I do in this case and why page do not want to load with "itemValue"? – Zyngi Jan 10 '14 at 00:01
  • Try to use an array of SelectItem rather than String. It's pretty easy to wrap a String in a SelectItem, in this way you can safely remove both itemLabel and itemValue since JSF will take care of everything. – elbuild Jan 10 '14 at 11:59
  • Unfortunatelly we had to change our project to JSF 1.2. Do you know how is it possible to do now? Sorry for so much trouble. – Zyngi Jan 10 '14 at 16:18
  • My JSF 1.2 skills are a little rusty but the code should work with minor modifications. – elbuild Jan 10 '14 at 16:36