我面临着重置p:selectOneMenu的问题。在我的面部,我有两个p:selectOneMenu项目。我的要求是,如果用户从第一个p:selectOneMenu中选择什么,那么第二个p:selectOneMenu应该重置自己,反之亦然。
下面是我使用的代码:
<p:outputLabel for="country" value="Country:" />
<p:selectOneMenu id="country" effect="none"
value="#{infoBean.infoDataHolder.selectedCountry}">
<f:selectItem itemLabel="Select One" itemValue=""
noSelectionOption="true" />
<f:selectItems
value="#{infoBean.infoDataHolder.availableCountries}"
var="aCountry" itemLabel="#{aCountry.description}"
itemValue="#{aCountry.description}" />
<p:ajax update="state"
listener="#{infoBean.resetState()}" />
</p:selectOneMenu>
<p:outputLabel for="state" value="State:" />
<p:selectOneMenu id="state" effect="none"
value="#{infoBean.infoDataHolder.selectedState}">
<f:selectItem itemLabel="Select One" itemValue=""
noSelectionOption="true" />
<f:selectItems
value="#{infoBean.infoDataHolder.availableStates}"
var="aState" itemLabel="#{aState}"
itemValue="#{aState}" />
<p:ajax update="country"
listener="#{infoBean.resetCountry()}" />
</p:selectOneMenu>我的支持bean InfoBean在RequestScope中,infoDataHolder在视图范围内。在infoBean.resetCountry() / infoBean.resetState()中,我将infoBean.infoDataHolder.selectedCountry / infoBean.infoDataHolder.selectedState设为空。
现在发生的事情是,当我选择国家时,国家p:selectOneMenu被重新审查。但是选择国家,国家p:选择,getting没有被重新审查。你能帮帮我吗。谢谢。
发布于 2014-02-20 12:15:42
您可能希望更新选择一个菜单的父组件,如
<p:panel id="panel_">
<p:selectOneMenu id="country" ...
<p:ajax update="panel_" listener="#{infoBean.resetState()}" />
</p:selectOneMenu>
<p:selectOneMenu id="state" ...
<p:ajax update="panel_" listener="#{infoBean.resetCountry()}" />
</p:selectOneMenu>
</p:panel> 发布于 2014-02-20 12:27:21
我看不出你在选择一个州时需要重新设定国家名单的意义。我认为,这里的适当行为是允许最终用户在每个国家内选择一个国家。这是通过加载每个国家的相关状态并呈现依赖的h/p:selectOneMenu来实现的。
我不鼓励您为此使用两种不同的bean,只需使用@ViewScoped即可。另外,从视图中访问瞬态JSF (#{infoBean.infoDataHolder})在JSF中没有意义,只是直接访问bean。
这是我的解决办法:
@ManagedBean
@ViewScoped
public class InfoDataHolder {
private List<String> availableCountries = Arrays.asList("USA",
"Switzerland");
private List<String> availableStates = new ArrayList<String>();
private String selectedCountry;
private String selectedState;
public void countrySelected() {
if ("USA".equals(selectedCountry)) {
availableStates = Arrays.asList("Arizona", "California");
} else if ("Switzerland".equals(selectedCountry)) {
availableStates = Arrays.asList("Zurich", "Bern");
} else {
availableStates = new ArrayList<String>();
}
}
public List<String> getAvailableCountries() {
return availableCountries;
}
public List<String> getAvailableStates() {
return availableStates;
}
public String getSelectedCountry() {
return selectedCountry;
}
public String getSelectedState() {
return selectedState;
}
public void setSelectedCountry(String selectedCountry) {
this.selectedCountry = selectedCountry;
}
public void setSelectedState(String selectedState) {
this.selectedState = selectedState;
}
}<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head />
<h:body>
<h:form>
<h:selectOneMenu value="#{infoDataHolder.selectedCountry}">
<f:selectItem noSelectionOption="true" itemLabel="Choose a Country" />
<f:selectItems var="country"
value="#{infoDataHolder.availableCountries}" itemValue="#{country}" />
<f:ajax listener="#{infoDataHolder.countrySelected}"
render="state_selection" />
</h:selectOneMenu>
<h:selectOneMenu value="#{infoDataHolder.selectedState}"
id="state_selection">
<f:selectItem noSelectionOption="true" itemLabel="Choose an State" />
<f:selectItems value="#{infoDataHolder.availableStates}" var="state"
itemValue="#{state}" />
</h:selectOneMenu>
</h:form>
</h:body>
</html>还请参见:
https://stackoverflow.com/questions/21902009
复制相似问题