0
我正在研究基於JSF 2.0中的ajax請求的狀態城市下拉菜單。問題是我想重用這個jsf模塊(beans和xhtmls)以供將來實現。可重複使用的JSF 2包括問題
是否有任何解決方案可以使parentBean更容易從另一個文件中實現的下拉列表中檢索id?
它出現在我腦海中的第一個解決方案是使用ui:include,但是我在存儲最終選定選項的變量中遇到空值。
以下是摘錄。的基本結構是:
- StateCityBean(Ajax的菜單控制器)
- stateCityBean.xhtml(Ajax的菜單)
- ParentBean(selectedCity變量這裏使用)
- 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"
}
}
我會推薦另一種方法,你應該創建一個自定義組件和一個託管bean來管理,然後將其注入到你的願望MB中。 –
@JorgeCampos但我這樣做就是這樣。 stateCityBean和stateCityBean.xhtml是「組件」,你不覺得嗎?或者你的意思是一個JSF組件? –
差不多。 JSF複合組件。這裏有一個例子:https://www.tutorialspoint.com/jsf/jsf_composite_components.htm –