Question

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?

Was it helpful?

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.

OTHER TIPS

Although what you choose in your drop-down list, the item's value will be the frist line

<h:selectOneMenu value="#{meslek}">

So, let's declare a variable with name "meslek" and getter/setter to use the value.

private String meslek;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top