可重用 JSF 2 包含问题

Reusable JSF 2 include problems

我正在基于 JSF 2.0 中的 ajax 请求开发州-城市下拉菜单。问题是我想在未来的实现中重用这个 jsf 模块(bean 和 xhtml)。

是否有任何解决方案可以使 parentBean 从另一个文件中实现的下拉列表中检索 id 变得容易?

我想到的第一个解决方案是使用 ui:include,但我在存储最终选定选项的变量中遇到空值。

这是片段。基本结构是:

  1. StateCityBean(Ajax 菜单控制器)
  2. stateCityBean.xhtml(Ajax 菜单)
  3. ParentBean(这里使用selectedCity变量)
  4. parentBean.xhtml(ui:include到stateCityBean.xhtml在这里)

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

}

好的。我解决了问题。

显然是 s

stateCity.xhtml 我删除了:

<t:subform id="stateCitySelectMenu">

就是这样。