如何从selectOneMenu获取价值

时间:2013-09-02 10:06:53

标签: jsf managed-bean selectonemenu

我必须得到我从JSF中的selectOneMenu中选择的值。我使用了一个ArrayList,我手里拿着数据。

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

这是我的豆子;

 @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 String getSonuc() {
    return sonuc;
}

public void setSonuc(String sonuc) {
    this.sonuc = sonuc;
}


public String getAd() {
    return ad;
}

public void setAd(String ad) {
    this.ad = ad;
}

public String getSoyad() {
    return soyad;
}

public void setSoyad(String soyad) {
    this.soyad = soyad;
}

public String getCinsiyet() {
    return cinsiyet;
}

public void setCinsiyet(String cinsiyet) {
    this.cinsiyet = cinsiyet;
}

public ArrayList<SelectItem> getSelectıtem() {
    return selectıtem;
}

public void setSelectıtem(ArrayList<SelectItem> selectıtem) {
    this.selectıtem = selectıtem;
}

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

public String getDonenMeslek() {
    return donenMeslek;
}

public void setDonenMeslek(String donenMeslek) {
    this.donenMeslek = donenMeslek;
}

}

它显示了arraylist数据,但我无法从那里获得价值。如何通过Javabean从这个selectOneMenu中获取值?

2 个答案:

答案 0 :(得分:2)

您应该阅读这些教程:

  1. Mkyoung selectOneMenu tutorial
  2. Mkyong converter tutorial
  3. 基本上你应该为selectOneMenu标签选择值容器。它意味着它应该看起来像:

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

    如果你需要传递一个不是字符串的对象,你应该使用转换器。看看顶部的链接。

答案 1 :(得分:1)

虽然您在下拉列表中选择了什么,但该项的值将是第3行

<select class="form-control" id="H_MEALS_SC" >
  <option value=''>Select One</option>
  <option value="1" > 1 - Unit/servings</option>
  <option value="2" > 2 - Sachet</option>
  <option value="3" > 3 - Pack</option>
</select>
     <input type="hidden" name="H_MEALS_SC" id="H_MEALS_SC" value="1" />

所以,让我们声明一个名为&#34; meslek&#34;的变量。和getter / setter使用该值。

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