簡體   English   中英

可重用的JSF 2包含問題

[英]Reusable JSF 2 include problems

我正在基於JSF 2.0中的ajax請求進行State-City Dropdown。 問題是我想將此jsf模塊(bean和xhtmls)重用於以后的實現。

有什么解決方案可以使parentBean輕松地從另一個文件中實現的下拉菜單中檢索ID?

我想到的第一個解決方案是使用ui:include,但我在存儲最終選定選項的變量上遇到了空值。

這是代碼段。 基本結構是:

  1. StateCityBean(Ajax菜單控制器)
  2. stateCityBean.xhtml(Ajax菜單)
  3. ParentBean(此處使用selectedCity變量)
  4. parentBean.xhtml(ui:包括到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"
}

}

好。 我解決了問題。

顯然是

stateCity.xhtml我刪除了:

<t:subform id="stateCitySelectMenu">

就是這樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM