请参阅此primefaces下拉链接https://www.primefaces.org/showcase/ui/ajax/dropdown.xhtml,我已经用它来成功建立国家和州关系。我刚刚被要求添加一个城市作为对状态的另一个依赖,我想知道如何构建城市的hashmap?
有人能给我一些帮助吗?我非常喜欢primefaces,jsf和hibernator以及所有这些,非常感谢你能帮助我看看这个。
Primefaces 3.4.2,JSF2.1.29
public void onCountryChange() {
if(country !=null && !country.equals(""))
cities = data.get(country);
else
cities = new HashMap<String, String>();
}
<h:form>
<p:growl id="msgs" showDetail="true" />
<p:panel header="Select a Location" style="margin-bottom:10px;">
<h:panelGrid columns="2" cellpadding="5">
<p:outputLabel for="country" value="Country: " />
<p:selectOneMenu id="country" value="#{dropdownView.country}" style="width:150px">
<p:ajax listener="#{dropdownView.onCountryChange}" update="city" />
<f:selectItem itemLabel="Select Country" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{dropdownView.countries}" />
</p:selectOneMenu>
<p:outputLabel for="city" value="City: " />
<p:selectOneMenu id="city" value="#{dropdownView.city}" style="width:150px">
<f:selectItem itemLabel="Select City" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{dropdownView.cities}" />
</p:selectOneMenu>
</h:panelGrid>
<p:separator />
<p:commandButton value="Submit" update="msgs" actionListener="#{dropdownView.displayLocation}" icon="ui-icon-check" />
</p:panel>
@ManagedBean
@ViewScoped
public class DropdownView implements Serializable {
private Map<String,Map<String,String>> data = new HashMap<String, Map<String,String>>();
private String country;
private String city;
private Map<String,String> countries;
private Map<String,String> cities;
@PostConstruct
public void init() {
countries = new HashMap<String, String>();
countries.put("USA", "USA");
countries.put("Germany", "Germany");
countries.put("Brazil", "Brazil");
Map<String,String> map = new HashMap<String, String>();
map.put("New York", "New York");
map.put("San Francisco", "San Francisco");
map.put("Denver", "Denver");
data.put("USA", map);
map = new HashMap<String, String>();
map.put("Berlin", "Berlin");
map.put("Munich", "Munich");
map.put("Frankfurt", "Frankfurt");
data.put("Germany", map);
map = new HashMap<String, String>();
map.put("Sao Paolo", "Sao Paolo");
map.put("Rio de Janerio", "Rio de Janerio");
map.put("Salvador", "Salvador");
data.put("Brazil", map);
}
我想我得上班了:
countries1 = new HashMap<String, String>();
countries1.put("USA", "USA");
countries1.put("Malaysia", "Malaysia");
Map<String,String> map = new HashMap<String, String>();
map.put("Washington", "Washington");
map.put("California", "California");
data.put("USA", map);
map = new HashMap<String, String>();
map.put("Johor", "Johor");
map.put("Penang", "Penang");
data.put("Malaysia", map);
Map<String,String> map1 = new HashMap<String, String>();
map1.put("Washington City1", "Washington City1");
map1.put("Washington City2", "Washington City2");
data1.put("Washington", map1);
map1 = new HashMap<String, String>();
map1.put("California City1", "California City1");
map1.put("California City2", "California City2");
data1.put("California", map1);
map1 = new HashMap<String, String>();
map1.put("Johor City1", "Johor City1");
map1.put("Johor City2", "Johor City2");
data1.put("Johor", map1);
map1 = new HashMap<String, String>();
map1.put("Penang City1", "Penang City1");
map1.put("Penang City2", "Penang City2");
data1.put("Penang", map1);
-
<h:outputLabel for="country_id1" value="#{msg.countryLabel}: *" />
<p:selectOneMenu id="country_id1" value="#{userBean.country1}"
style="width:150px">
<p:ajax listener="#{userBean.onCountryChange}" update="state2" />
<f:selectItem itemLabel="Select Country" itemValue=""
noSelectionOption="false" />
<f:selectItems value="#{userBean.countries1}" />
</p:selectOneMenu>
<p:message for="country_id1" />
<h:outputLabel for="state2" value="State: *" />
<p:selectOneMenu id="state2" value="#{userBean.state}"
style="width:150px">
<p:ajax listener="#{userBean.onStateChange}" update="city22" />
<f:selectItem itemLabel="Select State" itemValue=""
noSelectionOption="false" />
<f:selectItems value="#{userBean.states}" />
</p:selectOneMenu>
<p:message for="state2" />
<h:outputLabel for="city22" value="City: *" />
<p:selectOneMenu id="city22" value="#{userBean.city1}"
style="width:150px">
<f:selectItem itemLabel="Select City" itemValue=""
noSelectionOption="true" />
<f:selectItems value="#{userBean.cities1}" />
</p:selectOneMenu>
public void onCountryChange() {
lg.info("country in change: " + country1);
if (country1 != null && !country1.equals("")) {
states = data.get(country1);
}
else
states = new HashMap<String, String>();
}
public void onStateChange() {
lg.info("state in change: " + state);
if (state != null && !state.equals("")) {
cities1 = data1.get(state);
}
else
cities1 = new HashMap<String, String>();
}