简体   繁体   中英

Reusable JSF 2 include problems

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:

  1. StateCityBean (Ajax Menu controller)
  2. stateCityBean.xhtml (Ajax Menu)
  3. ParentBean (selectedCity variable is used here)
  4. 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"
}

}

Ok. I solved the problem.

Apparently was the s

at stateCity.xhtml i removed:

<t:subform id="stateCitySelectMenu">

And that's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM