Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8153993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:11:55+00:00 2026-06-06T16:11:55+00:00

I am trying to populate a dropdown list when some other drop down gets

  • 0

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.

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

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:11:59+00:00Added an answer on June 6, 2026 at 4:11 pm

    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.

    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.

    See also:

    • Why JSF calls getters multiple times
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to auto-populate a drop down list based on a request parameter. I'm
I am trying to populate a drop down list when a specific list item
Background - I'm trying to populate a drop down list with state information from
I am trying to populate a second dropdown list based on the value of
I'm trying to populate a second drop down list (which contains counties) depending on
I trying to populate the dropdown list when page was loaded.But it is not
I am just trying to populate a html.dropdown list using mvc2 in VS2008. But
I'm trying to create a drop down list that populates from a database. I
I am using ExtJS (3) and just trying to populate a combobox/drop down using
I'm trying to populate a dropdown list inside a repeater, but I'm not being

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.