Dropdown list is not populating in JSF

450 Views Asked by At

I am working on Managedbeans and JSF. As shown below that my ManagedBean contains all the requirements that are required for the JSF to get the value. I have initialised my dropdown list as below. In selectOneMenu, I have chosen the country as a string where it will store the value selected by the dropdown list and the dropdown list will bring up the list that I declared in the Beans. Unfortunately, it is not happening like that. Every time dropdown renders it gives me an empty value. I have spent days on it but cannot figure out the exact solution to it. I have cleaned my server, build workspace and also change servers but nothing is working.

** ManagedBean_list **

private List<String> listCountry;   
    private String country;
        public void tada(){
        listCountry=Arrays.asList("India", "pakisatan","America");
    }   
    public List<String> getListCountry() {
        return listCountry;
    }
    public void setListCountry(List<String> listCountry) {
        this.listCountry = listCountry;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }

JSF

<p:selectOneMenu id="country" value="#{loginBeans.country}">
<f:selectItems value="#{loginBeans.listCountry}"  />
</p:selectOneMenu>

Your help is appreciated. Empty dropdown list image enter image description here

3

There are 3 best solutions below

0
Nashrah Khan On BEST ANSWER

The issue is that on initialization, the list is not being called up. I resolved it by including the list function inside the constructor of managed beans class. so that when the constructor fired up. It also generates the dropdown list.

2
zulqarnain On

Either convert your listCountry to a

    private Map<String, String> listCountry = new HashMap<>();
    listCountry.put("India", "India");
    listCountry.put("Pakistan", "Pakistan");
    listCountry.put("America", "America");

or

private List<SelectItem> listCountry = new ArrayList<>();
listCountry.add(new SelectItem("India", "India"));
listCountry.add(new SelectItem("Pakistan", "Pakistan"));
listCountry.add(new SelectItem("America","America"));
2
Manfred Riem On

Which bean annotation are you using? You say "Managedbeans", but the source you posted does not show the entire bean, or does it? Check to make sure you are not mixing old style JSF managed bean annotations with CDI annotations.