By Chris http://jroller.com/page/cschalk? Entry = building_dependent_select_menus_in
Translation: icess http://blog.matrix.org.cn/page/icess
In web programs, you sometimes need to manage the selection button. (Note: for example, when registering a QQ number, you must select a province or region. The region varies dynamically according to the province) there are multiple ways to implement this selection button in JSF (Note: You can use ajax4jsf to implement more friendly components). Here we provide a simple implementation.
Defining the hashmaps as managed beans
Faces provides the ability to directly declare lists and maps as managed beans in the configuration file. once defined in the faces-config.xml, faces apps can use her through El expressions, we define the following maps
Countriesmap-A map of countries: France, United States, India.
This map will drive some maps: uscitiesmap, frcitiesmap and incitiesmap.
When you select a country name, the dependent list is automatically loaded based on the country name.
The hashmaps
Countriesmap
<managed-bean> <managed-bean-name>CountriesMap</managed-bean-name> <managed-bean-class>java.util.HashMap</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <map-entries> <map-entry> <key>France</key> <value>fr</value> </map-entry> <map-entry> <key>United States</key> <value>us</value> </map-entry> <map-entry> <key>India</key> <value>in</value> </map-entry> </map-entries> </managed-bean>
|
And the dependent city maps ..
Uscitiesmaps
<managed-bean> <managed-bean-name>UsCitiesMap</managed-bean-name> <managed-bean-class>java.util.HashMap</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <map-entries> <map-entry> <key>New York</key> <value>ny</value> </map-entry> <map-entry> <key>San Francisco</key> <value>sf</value> </map-entry> <map-entry> <key>Los Angeles</key> <value>la</value> </map-entry> </map-entries> </managed-bean>
|
Frcitiesmaps
<managed-bean> <managed-bean-name>FrCitiesMap</managed-bean-name> <managed-bean-class>java.util.HashMap</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <map-entries> <map-entry> <key>Paris</key> <value>ps</value> </map-entry> <map-entry> <key>Lyon</key> <value>ly</value> </map-entry> <map-entry> <key>Orlean</key> <value>ol</value> </map-entry> </map-entries> </managed-bean>
|
Incitiesmap
<managed-bean> <managed-bean-name>InCitiesMap</managed-bean-name> <managed-bean-class>java.util.HashMap</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <map-entries> <map-entry> <key>New Delhi</key> <value>nd</value> </map-entry> <map-entry> <key>Bangalore</key> <value>bl</value> </map-entry> <map-entry> <key>Calcutta</key> <value>cc</value> </map-entry> </map-entries> </managed-bean>
|
Before selecting a country, we provide an emptycities map to display some prompts.
Emptycitiesmap
<managed-bean> <managed-bean-name>EmptyCitiesMap</managed-bean-name> <managed-bean-class>java.util.HashMap</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <map-entries> <map-entry> <key>empty</key> <value>em</value> </map-entry> </map-entries> </managed-bean>
|
Note that these maps can also be dynamically created and driven from other locations, such as databases.
The following JSP code contains two select menus statements.
<%@ page contentType="text/html;charset=windows-1252"%> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%> <f:view> <title>deplist</title> <body> <p> Country: binding="#{Deplist_backing.inputCountry}" > <f:selectItems value="#{CountriesMap}"/> </p> <p> City: <f:selectItems value="#{Deplist_backing.cities}"/> </p> </f:view> |
The page contains twoSelectonemenuComponents and their child componentsSelectitemsBound to the faces configuration file# {Countriesmap}And a list of cities changed by the selected country name.# {Deplist_backing.cities}Deplist_backingThe object is a managed bean Code as follows:
package backing;import java.util.Map;import javax.faces.component.html.HtmlSelectOneMenu;import javax.faces.context.FacesContext;import javax.faces.el.ValueBinding;public class DepList { HtmlSelectOneMenu inputCountry, inputCity; // Method which returns the cities Map // based on the value of inputCountry public Map getCities() { String cityMap = ""; if (inputCountry.getValue() != null) { String countryKey = inputCountry.getValue().toString(); if (countryKey.equals("fr")) cityMap = "FrCitiesMap"; else if (countryKey.equals("us")) cityMap = "UsCitiesMap"; else if (countryKey.equals("in")) cityMap = "InCitiesMap"; else cityMap = "EmptyCitiesMap"; } else { cityMap = "EmptyCitiesMap"; } cityMap = "#{" + cityMap + "}"; //retrieve correct cityMap and return FacesContext context = FacesContext.getCurrentInstance( ); ValueBinding binding; binding = context.getApplication().createValueBinding(cityMap); return (Map) binding.getValue(context); } public void setInputCountry(HtmlSelectOneMenu inputCountry) { this.inputCountry = inputCountry; } public HtmlSelectOneMenu getInputCountry() { return inputCountry; } public void setInputCity(HtmlSelectOneMenu inputCity) { this.inputCity = inputCity; } public HtmlSelectOneMenu getInputCity() { return inputCity; }} |
Note that the getcities method checks whether there are input values in inputcountry. If yes, It means loading the city map based on the selected country. This is done by accessing the city map in the managed bean ..
And finally the registration of the classDeplistAs managed BeanDeplist_backing.
<managed-bean> <managed-bean-name>Deplist_backing</managed-bean-name> <managed-bean-class>backing.DepList</managed-bean-class> <managed-bean-scope>request</managed-bean-scope></managed-bean> |
And that's it .. when you run the JSP page, you will be able to select the country first and then select the city name.