Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF 1.2: Options for dropdown list not updated when it is set for the first time
    primarykey
    data
    text
    <p>I have written a program using jsf 1.2 . In this program a user needs to select his <strong>country, state</strong>, and <strong>city</strong>. When he selects a <strong>country</strong>, he must see the list of states in it in a dropdown &amp; when he selects a state, he must see the cities in it in a dropdown.</p> <p>For that I have created 2 classes, 1 inner class , and one jsf page.</p> <p>When I select a country , I can see a dropdown for state is immediately shown.</p> <p>Problem is when a state is changed, the drop down for city is not immediately seen. Only after 2nd change in state, the drop down for cities is seen.</p> <p>(Note: I cannot remove the method <code>cityChangeListener()</code> in this code , as i need to generate an id using country-code,state-code,city-code,and a random number. )</p> <p>Any help would be highly appreciated.</p> <p>Here is my code (4 files):</p> <p>file: faces-config.xml</p> <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt; &lt;faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"&gt; &lt;managed-bean&gt; &lt;managed-bean-name&gt;formBackingBean&lt;/managed-bean-name&gt; &lt;managed-bean-class&gt;FormBackingBean&lt;/managed-bean-class&gt; &lt;managed-bean-scope&gt;session&lt;/managed-bean-scope&gt;&lt;/managed-bean&gt; &lt;/faces-config&gt; </code></pre> <p>file: newjsf.jsp</p> <pre><code>&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt; &lt;%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%&gt; &lt;%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%&gt; &lt;f:view&gt; &lt;h:form&gt; &lt;h:inputText id="input" value="#{formBackingBean.input}" immediate="true" disabled="true" /&gt; &lt;br/&gt; &lt;h:selectOneMenu id="country" value="#{formBackingBean.country}" valueChangeListener="#{formBackingBean.countryChangeListener}" immediate="true" onchange="submit();" &gt; &lt;f:selectItems id="countries" value="#{formBackingBean.countries}" /&gt; &lt;/h:selectOneMenu&gt; &lt;br/&gt; &lt;h:selectOneMenu id="state" value="#{formBackingBean.state}" valueChangeListener="#{formBackingBean.stateChangeListener}" immediate="true" onchange="submit();" rendered="#{formBackingBean.states!=null}" &gt; &lt;f:selectItems id="states" value="#{formBackingBean.states}" /&gt; &lt;/h:selectOneMenu&gt; &lt;br/&gt; &lt;h:selectOneMenu id="city" value="#{formBackingBean.city}" immediate="true" onchange="submit();" rendered="#{formBackingBean.cities!=null}" valueChangeListener="#{formBackingBean.cityChangeListener}" &gt; &lt;f:selectItems id="cities" value="#{formBackingBean.cities}" /&gt; &lt;/h:selectOneMenu&gt; &lt;/h:form&gt; &lt;/f:view&gt; </code></pre> <p>file: FormBackingBean.java</p> <pre><code>import java.util.List; import javax.faces.context.FacesContext; import javax.faces.event.ValueChangeEvent; import javax.faces.model.SelectItem; public class FormBackingBean { private String input; private String country; private String state; private String city; private List&lt;SelectItem&gt;countries=GeoMapLister.WORLD.getNamesOfPlacesWithin();//MapLister.countryList; private List&lt;SelectItem&gt;states=null; private List&lt;SelectItem&gt;cities=null; public FormBackingBean() { } public String getCity() { return city; } public String getCountry() { return country; } public String getState() { return state; } public void setCity(String city) { this.city = city; FacesContext.getCurrentInstance().renderResponse(); } public void setCountry(String country) { this.country = country; FacesContext.getCurrentInstance().renderResponse(); } public void setState(String state) { this.state = state; } public List&lt;SelectItem&gt; getCities() { return cities; } public List&lt;SelectItem&gt; getCountries() { return countries; } public List&lt;SelectItem&gt; getStates() { return states; } public void setCities(List&lt;SelectItem&gt; cities) { this.cities = cities; } public void setCountries(List&lt;SelectItem&gt; countries) { this.countries = countries; } public void setStates(List&lt;SelectItem&gt; states) { this.states = states; } public String getInput() { return input; } public void setInput(String input) { this.input = input; } public void countryChangeListener(final ValueChangeEvent valueChangeEvent){ input=valueChangeEvent.getNewValue().toString(); states=GeoMapLister.WORLD.getPlacesWithin().get(input).getNamesOfPlacesWithin(); state=null; cities=null; city=null; FacesContext.getCurrentInstance().renderResponse(); } public void stateChangeListener(final ValueChangeEvent valueChangeEvent){ input=valueChangeEvent.getNewValue().toString(); cities=GeoMapLister.WORLD.getPlacesWithin() .get(country).getPlacesWithin().get(input) .getNamesOfPlacesWithin(); city=null; FacesContext.getCurrentInstance().renderResponse(); } public void cityChangeListener(final ValueChangeEvent valueChangeEvent){ input=valueChangeEvent.getNewValue().toString(); FacesContext.getCurrentInstance().renderResponse(); } } </code></pre> <p>file: GeoMapLister.java</p> <pre><code>import java.util.ArrayList; import java.util.HashMap; import java.util.List; import javax.faces.model.SelectItem; public class GeoMapLister { public static class GeographicalLocation{ private final GeographicalLocation parent; private final String code; private final String name; private final List&lt;SelectItem&gt;namesOfPlacesWithin; private final HashMap&lt;String,GeographicalLocation&gt;placesWithin; public GeographicalLocation(GeographicalLocation parent,String code, String name,boolean leaf) { this.parent = parent; this.name = name; this.code= code; ArrayList&lt;SelectItem&gt; namesofplaceswithin = null; HashMap&lt;String,GeographicalLocation&gt; placeswithin = null; if(!leaf){ namesofplaceswithin = new ArrayList&lt;SelectItem&gt;(); namesofplaceswithin.add(new SelectItem(null, "- select -")); placeswithin = new HashMap&lt;String,GeographicalLocation&gt;(); placeswithin.put(null, null); } this.namesOfPlacesWithin = namesofplaceswithin; this.placesWithin = placeswithin; } public String getName() { return name; } public GeographicalLocation getParent() { return parent; } public List&lt;SelectItem&gt; getNamesOfPlacesWithin() { return namesOfPlacesWithin; } public String getCode() { return code; } public GeographicalLocation addPlaceWithin(String code,String name,boolean leaf){ GeographicalLocation newGeographicalLocation = new GeographicalLocation(this, code,name,leaf); placesWithin.put(code,newGeographicalLocation); namesOfPlacesWithin.add(new SelectItem(code, name)); return newGeographicalLocation; } public HashMap&lt;String, GeographicalLocation&gt; getPlacesWithin() { return placesWithin; } List&lt;SelectItem&gt; getPlacesWithin(String input) { return placesWithin.get(input).namesOfPlacesWithin; } } public static final GeographicalLocation WORLD=new GeographicalLocation(null, "WORLD","WORLD",false); static { GeographicalLocation IN = WORLD.addPlaceWithin("IN", "India", false); GeographicalLocation MH = IN.addPlaceWithin("MH", "Maharashtra", false); MH.addPlaceWithin("MU", "Mumbai", true); MH.addPlaceWithin("PU", "Pune", true); MH.addPlaceWithin("NA", "Nashik", true); GeographicalLocation KA = IN.addPlaceWithin("KA", "Karnataka", false); KA.addPlaceWithin("BA", "Bangalore", true); KA.addPlaceWithin("MA", "Mangalore", true); GeographicalLocation BE = IN.addPlaceWithin("BE", "West Bengal", false); BE.addPlaceWithin("KO", "Kolkata", true); BE.addPlaceWithin("KH", "Kharagpur", true); //-------------- GeographicalLocation UK = WORLD.addPlaceWithin("UK", "U.K.", false); GeographicalLocation YO = UK.addPlaceWithin("YO", "York", false); YO.addPlaceWithin("SS", "Sussex", true); YO.addPlaceWithin("MS", "Middlesex", true); //-------------- GeographicalLocation US = WORLD.addPlaceWithin("US", "U.S.A.", false); GeographicalLocation NY = US.addPlaceWithin("NY", "New York", false); NY.addPlaceWithin("NC", "New York city", true); NY.addPlaceWithin("NU", "New York urban", true); GeographicalLocation CA = US.addPlaceWithin("CA", "California", false); CA.addPlaceWithin("WA", "Washington, district of California", true); CA.addPlaceWithin("SI", "Silicon Valley", true); CA.addPlaceWithin("VS", "Very-Silly-Con Valley", true); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload