I'm working on a State-City Dropdown based on ajax requests in JSF 2.0. The thing is that i want to reuse this jsf module (beans and xhtmls) for future implementations.
Is there any solution that make it easy to the parentBean retrieve the id from a dropdown that is implemented in another file ?
The first solution that it came in my head was using ui:include but i'm experiencing null value at the variable that store the final selected option.
Here is the snippet. The basic structure is:
- StateCityBean (Ajax Menu controller)
- stateCityBean.xhtml (Ajax Menu)
- ParentBean (selectedCity variable is used here)
- parentBean.xhtml (ui:include to stateCityBean.xhtml is here)
stateCityBean
@ManagedBean(name = "stateCityBean")
@ViewScoped
public class StateCityBean implements Serializable {
private static final long serialVersionUID = 7344375121014662582L;
private static final Logger LOG = Logger.getLogger(MenuEstadoCidadeBean.class);
private Collection<SelectItem> stateList;
private Collection<SelectItem> stateCity;
private String selectedState; //not used yet, test purpose
private Integer selectedCity; //not used yet, test purpose
//All the code here is perfectly fine. Cities are loading based on the selected state in parentBean.
}
stateCityBean.xhtml
<t:subform id="stateCitySelectMenu">
<span class="label tamanho20">STATE:</span>
<span aria-live="polite">
<h:selectOneMenu class="tamanho10" id="stateList" name="stateList" value="#{bean.selectedState}" title="State">
<f:selectItems value="#{stateCityBean.stateList}" />
<f:ajax event="valueChange" render="CityList" listener="#{stateCityBean.searchCities(bean.selectedState)}" onevent="loadingGif"/>
</h:selectOneMenu>
</span>
<span class="tamanho20">CITY:</span>
<span aria-live="polite">
<h:selectOneMenu class="tamanho20" title="City" id="CityList" name="CityList" value="#{bean.selectedCity}">
<f:selectItems value="#{stateCityBean.cityList}" />
</h:selectOneMenu>
</span>
</t:subform>
parentBean.xhtml
<ui:include src="stateCityBean.xhtml">
<ui:param name="bean" value="#{parentBean}" />
</ui:include>
parentBean.java
@ManagedBean
@RequestScoped
public class parentBean implements Serializable {
private static final long serialVersionUID = -874412419784226660L;
private static final Logger LOG = Logger.getLogger(parentBean.class);
String selectedState;
Integer selectedCity;
public String getSelectedState() {
return selectedState;
}
public void setSelectedState(String selectedState) {
this.selectedState = selectedState;
}
public Integer getSelectedCity() {
return selectedCity;
}
public void setSelectedCity(Integer selectedCity) {
this.selectedCity= selectedCity;
}
public void doSomethingWithTheSelectedCityId(){
//bla bla bla
return "anotherPage"
}
}