Question

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.

Was it helpful?

Solution

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.

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