0

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"
}

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Max
  • 478
  • 4
  • 11
  • I would recommend another approach, you should create a custom component and one managed bean to manage, then just inject it on your MB of your desire. – Jorge Campos Aug 21 '17 at 21:38
  • @JorgeCampos But i am doing it just like that. stateCityBean and stateCityBean.xhtml are the "component", dont you think ? or do you mean a JSF Component ? – Max Aug 21 '17 at 21:40
  • Almost that. A JSF composite component. Here is one example: https://www.tutorialspoint.com/jsf/jsf_composite_components.htm – Jorge Campos Aug 21 '17 at 21:44
  • Or this one: https://stackoverflow.com/questions/6822000/when-to-use-uiinclude-tag-files-composite-components-and-or-custom-componen/6822269#6822269 – Kukeltje Aug 22 '17 at 07:09
  • I think the issue is not related to component creation itself, but how the selected id in the DropDown menu, at stateCityBean.xhtml, is passed through parentBean. Thats the real problem. Pasting the dropdown html inside parentBean.xhtml it seems to work. Using ui:include and the dropdown html into stateCityBean.xhtml, dont. – Max Aug 22 '17 at 13:53

1 Answers1

0

Ok. I solved the problem.

Apparently was the s

at stateCity.xhtml i removed:

<t:subform id="stateCitySelectMenu">

And that's it.

Max
  • 478
  • 4
  • 11