h:selectOneMenu没有填充在jsf中的f:ajax上

时间:2022-10-23 20:00:55

I am trying to populate a dropdown list when some other drop down gets changed its value. i.e. the popular example: country and city. I tried the f:ajax for this. well the ajax call is happening but the city dropdown is not getting populated.

当其他一些下拉列表更改其值时,我正在尝试填充下拉列表。即流行的例子:国家和城市。我为此尝试了f:ajax。好的ajax电话正在发生,但城市下降不会填充。

There is some problem in my Managed Bean code itself, but I can't find it. Can someone take a look?

我的Managed Bean代码本身存在一些问题,但我找不到它。有人可以看看吗?

register.xhtml

    <h:selectOneMenu id="state" value="#{registerBean.state}" required="true">                    
          <f:selectItems value="#{registerBean.stateList}"/>
       <f:ajax render="outputDrop city" listener="#{registerBean.cityListener}"/>                                                            
     </h:selectOneMenu>                    
                <h:message for="state" />

                <h:outputText id="outputDrop" value="#{registerBean.state}" />

                <h:outputText value="#{msgbundle.reg_city}" />
      <h:selectOneMenu id="city" value="#{registerBean.city}" required="true">
                   <f:selectItems value="#{registerBean.cityList}"  />
                </h:selectOneMenu>
                <h:message for="city" />

ManagedBean

package org.droidaceapps.contractortimeflow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.model.SelectItem;

public class RegUserInfoBean {
   private String userName;
   private String city;   
   private String state;
   private String phone;
   private String sex;

   private ArrayList<SelectItem> stateList;
   private ArrayList<SelectItem> cityList;

   private enum stateKeys {AP,TN,MH};

   public ArrayList<SelectItem> getStateList(){
       stateList = new ArrayList<SelectItem>();
   stateList.add(new SelectItem(null,"Select state"));
   stateList.add(new SelectItem("AP","Andhra Pradesh"));
   stateList.add(new SelectItem("TN","Tamilnadu"));
   stateList.add(new SelectItem("MH","Maharastra"));
   return stateList;
}

  public ArrayList<SelectItem> getCityList(){
   cityList = new ArrayList<SelectItem>();
   cityList.add(new SelectItem(null,"Select"));
   return cityList;
   }

  public void cityListener(AjaxBehaviorEvent event){

   switch(stateKeys.valueOf(state)){
   case AP:
          cityList.add(new SelectItem("VIJ","Vijayawada"));
          cityList.add(new SelectItem("GUN","Guntur"));           
          break;
   case MH:
          cityList.add(new SelectItem("MUM","Mumbai"));
          cityList.add(new SelectItem("PUN","Pune"));             
          break;
   case TN:
          cityList.add(new SelectItem("CHE","Chennai"));
          cityList.add(new SelectItem("MAD","Madurai"));              
          break;
    default:
        cityList.add(new SelectItem("NA","No value"));
   }

}


public String getSex() {
return sex;
 }
public void setSex(String sex) {
this.sex = sex;
 }
public String getUserName() {
return userName;
 }
 public void setUserName(String userName) {
this.userName = userName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
 }

 public String getState() {
return state;
 }
 public void setState(String state) {
this.state = state;
 }
public String getPhone() {
return phone;
 }
public void setPhone(String phone) {
this.phone = phone;
}

 }

1 个解决方案

#1


0  

You're recreating the citylist everytime in the getter method. You should not do that. The getter methods should not contain any business code. They should only return the property. You should be doing initialization in the constructor of the backing bean or a @PostConstruct method.

您每次都在getter方法中重新创建城市列表。你不应该这样做。 getter方法不应包含任何业务代码。他们应该只归还财产。您应该在辅助bean的构造函数或@PostConstruct方法中进行初始化。

So, replace

public ArrayList<SelectItem> getCityList(){
    cityList = new ArrayList<SelectItem>();
    cityList.add(new SelectItem(null,"Select"));
    return cityList;
}

by

@PostConstruct
public void init() {
    cityList = new ArrayList<SelectItem>();
    cityList.add(new SelectItem(null,"Select"));
}

public ArrayList<SelectItem> getCityList(){
    return cityList;
}

I'd also change the getStateList() method accordingly. Recreating the list on every getter method call is plain inefficient.

我还会相应地更改getStateList()方法。在每个getter方法调用上重新创建列表是非常低效的。

See also:

#1


0  

You're recreating the citylist everytime in the getter method. You should not do that. The getter methods should not contain any business code. They should only return the property. You should be doing initialization in the constructor of the backing bean or a @PostConstruct method.

您每次都在getter方法中重新创建城市列表。你不应该这样做。 getter方法不应包含任何业务代码。他们应该只归还财产。您应该在辅助bean的构造函数或@PostConstruct方法中进行初始化。

So, replace

public ArrayList<SelectItem> getCityList(){
    cityList = new ArrayList<SelectItem>();
    cityList.add(new SelectItem(null,"Select"));
    return cityList;
}

by

@PostConstruct
public void init() {
    cityList = new ArrayList<SelectItem>();
    cityList.add(new SelectItem(null,"Select"));
}

public ArrayList<SelectItem> getCityList(){
    return cityList;
}

I'd also change the getStateList() method accordingly. Recreating the list on every getter method call is plain inefficient.

我还会相应地更改getStateList()方法。在每个getter方法调用上重新创建列表是非常低效的。

See also: