jsfmanaged-beanselectonemenu

How to retrieve selected value from selectOneMenu?


I need to get the value which I have selected from a selectOneMenu in JSF. I used an ArrayList and I am holding data in it.

<h:outputText value="Meslek : " />
    <h:selectOneMenu value="meslek">
        <f:selectItems id="meslek7" value="#{comyon.selectıtem}"/>
</h:selectOneMenu>

And this is my bean:

@ManagedBean(name = "comyon")
@RequestScoped

public class ComponentYonetim {

private String ad,soyad,cinsiyet;
private String sonuc = "";
private ArrayList<SelectItem> selectıtem = new ArrayList<SelectItem>();
private String donenMeslek = "";

public ComponentYonetim() {
    selectıtem.add(new SelectItem("Asker"));
    selectıtem.add(new SelectItem("Mühendis"));
    selectıtem.add(new SelectItem("Doktor"));
    selectıtem.add(new SelectItem("Öğrenci"));
    selectıtem.add(new SelectItem("Serbest meslek"));
    selectıtem.add(new SelectItem("Polis"));

}

public void yazdir(){
    if(this.ad.isEmpty() || this.soyad.isEmpty()){
        sonuc+="Lütfen boşluk bırakmayınız!";
    }
}

//setters and getters...

}

It's showing the ArrayList data but I can't get value from there. How can I get the value from this selectOneMenu with javaBean?


Solution

  • You should read those tutorials:

    1. Mkyoung selectOneMenu tutorial
    2. Mkyong converter tutorial

    Basically you should select value container for selectOneMenu tag. it means that it should look like:

    <h:selectOneMenu value="#{comyon.selectedItem}">
       <f:selectItems id="meslek7" value="#{comyon.selectıtem}"/>
    </h:selectOneMenu>
    

    and if you need to pass a object not string you should use converter. Look at the links on the top.