Primefaces 5.0 和 Ajax。未调用侦听器方法
Primefaces 5.0 and Ajax. Listener method not being called
我有多个使用 Primefaces 5.0 的下拉列表。当你 select 第一个下拉列表中的一个选项(在本例中是一个国家)时,它应该抓住那个国家的城市并填充城市下拉列表。但是,我有代码使用国家从我的数据库中填充我的城市下拉列表,但从未调用该方法。
我的Region.xhtml:
<h:form>
<p:tab title="Region Selection" rendered="true">
<p:panel header="Region Information">
<h:panelGrid columns="2" columnClasses="label, value">
<h:outputText value="Country" />
<p:selectOneMenu id="country" value="#{rosterBean.selectedCountry}">
<p:ajax listener="#{rosterBean.onCountryChange()}" update="state" />
<f:selectItem itemLabel="Select Country" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{rosterBean.countries}" />
</p:selectOneMenu>
<h:outputText value="State/ Province" />
<p:selectOneMenu id="state" value="#{rosterBean.selectedState}">
<p:ajax listener="#{rosterBean.onStateChange}" />
<f:selectItem itemLabel="Select State" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{rosterBean.states}" />
</p:selectOneMenu>
<h:outputText value="City" />
<p:selectOneMenu id="city" filter="true" filterMatchMode="contains" >
<f:selectItem itemLabel="Johannesburg" itemValue="0" noSelectionOption="true" />
<f:selectItem itemLabel="Pretoria" itemValue="1" noSelectionOption="true" />
</p:selectOneMenu>
</h:panelGrid>
</p:panel>
</p:tab>
</h:form>
我的豆子:
@ViewScoped
@ManagedBean
public class RosterBean implements Serializable {
public void onCountryChange() {
System.out.println("Updating State");
}
}
我已经获得了所有的 getter 和 setter,但是 system.out 从未被调用过。因此,无论它是否会呼叫该国家/地区的正确城市,它都永远不会到达那里。
您的输入元素和 p:ajax
需要嵌套在 h:form
中,否则不会在 POST 上提交任何数据。 actionListeners
有时需要访问提交的值(通过方法参数或 ActionEvent),因此如果提交的值不可用,似乎不会调用侦听器。
提示:您可以检查浏览器的开发人员工具以查看实际提交的内容。
另见
- JSF: Execute values of multiple forms
- How to execute another <h:form> using <f:ajax>?
- JSF - actionListener tag calls method which doesn't take an ActionEvent parameter
您似乎仍然遇到了一些问题,所以这是一个完整的示例,在您自己的基础上稍作修改:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
</h:head>
<h:body>
<h:form>
<p:panel header="Region Information">
<h:panelGrid columns="2" columnClasses="label, value">
<h:outputText value="Country" />
<p:selectOneMenu id="country" value="#{rosterBean.selectedCountry}" >
<p:ajax listener="#{rosterBean.onCountryChange()}" update="state" />
<f:selectItem itemLabel="Select Country" itemValue="" noSelectionOption="true" />
<f:selectItem itemLabel="Country 1" itemValue="1" />
</p:selectOneMenu>
<h:outputText value="State/ Province" />
<p:selectOneMenu id="state" value="#{rosterBean.selectedState}" >
<p:ajax listener="#{rosterBean.onCountryChange}" />
<f:selectItem itemLabel="Select State" itemValue="" noSelectionOption="true" />
<f:selectItem itemLabel="State1 1" itemValue="1" />
</p:selectOneMenu>
</h:panelGrid>
</p:panel>
</h:form>
<p:commandButton value="Next"/>
</h:body>
</html>
(请注意,我从 commandButton
中删除了 class
属性。class
不是 commandButton
上的有效属性。)
RosterBean.java
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class RosterBean {
public String selectedCountry;
public String selectedState;
public String getSelectedCountry() {
return selectedCountry;
}
public void setSelectedCountry(String selectedCountry) {
this.selectedCountry = selectedCountry;
}
public String getSelectedState() {
return selectedState;
}
public void setSelectedState(String selectedState) {
this.selectedState = selectedState;
}
public void onCountryChange() {
System.out.println("Updating State");
}
}
我有多个使用 Primefaces 5.0 的下拉列表。当你 select 第一个下拉列表中的一个选项(在本例中是一个国家)时,它应该抓住那个国家的城市并填充城市下拉列表。但是,我有代码使用国家从我的数据库中填充我的城市下拉列表,但从未调用该方法。
我的Region.xhtml:
<h:form>
<p:tab title="Region Selection" rendered="true">
<p:panel header="Region Information">
<h:panelGrid columns="2" columnClasses="label, value">
<h:outputText value="Country" />
<p:selectOneMenu id="country" value="#{rosterBean.selectedCountry}">
<p:ajax listener="#{rosterBean.onCountryChange()}" update="state" />
<f:selectItem itemLabel="Select Country" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{rosterBean.countries}" />
</p:selectOneMenu>
<h:outputText value="State/ Province" />
<p:selectOneMenu id="state" value="#{rosterBean.selectedState}">
<p:ajax listener="#{rosterBean.onStateChange}" />
<f:selectItem itemLabel="Select State" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{rosterBean.states}" />
</p:selectOneMenu>
<h:outputText value="City" />
<p:selectOneMenu id="city" filter="true" filterMatchMode="contains" >
<f:selectItem itemLabel="Johannesburg" itemValue="0" noSelectionOption="true" />
<f:selectItem itemLabel="Pretoria" itemValue="1" noSelectionOption="true" />
</p:selectOneMenu>
</h:panelGrid>
</p:panel>
</p:tab>
</h:form>
我的豆子:
@ViewScoped
@ManagedBean
public class RosterBean implements Serializable {
public void onCountryChange() {
System.out.println("Updating State");
}
}
我已经获得了所有的 getter 和 setter,但是 system.out 从未被调用过。因此,无论它是否会呼叫该国家/地区的正确城市,它都永远不会到达那里。
您的输入元素和 p:ajax
需要嵌套在 h:form
中,否则不会在 POST 上提交任何数据。 actionListeners
有时需要访问提交的值(通过方法参数或 ActionEvent),因此如果提交的值不可用,似乎不会调用侦听器。
提示:您可以检查浏览器的开发人员工具以查看实际提交的内容。
另见
- JSF: Execute values of multiple forms
- How to execute another <h:form> using <f:ajax>?
- JSF - actionListener tag calls method which doesn't take an ActionEvent parameter
您似乎仍然遇到了一些问题,所以这是一个完整的示例,在您自己的基础上稍作修改:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
</h:head>
<h:body>
<h:form>
<p:panel header="Region Information">
<h:panelGrid columns="2" columnClasses="label, value">
<h:outputText value="Country" />
<p:selectOneMenu id="country" value="#{rosterBean.selectedCountry}" >
<p:ajax listener="#{rosterBean.onCountryChange()}" update="state" />
<f:selectItem itemLabel="Select Country" itemValue="" noSelectionOption="true" />
<f:selectItem itemLabel="Country 1" itemValue="1" />
</p:selectOneMenu>
<h:outputText value="State/ Province" />
<p:selectOneMenu id="state" value="#{rosterBean.selectedState}" >
<p:ajax listener="#{rosterBean.onCountryChange}" />
<f:selectItem itemLabel="Select State" itemValue="" noSelectionOption="true" />
<f:selectItem itemLabel="State1 1" itemValue="1" />
</p:selectOneMenu>
</h:panelGrid>
</p:panel>
</h:form>
<p:commandButton value="Next"/>
</h:body>
</html>
(请注意,我从 commandButton
中删除了 class
属性。class
不是 commandButton
上的有效属性。)
RosterBean.java
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ManagedBean
@ViewScoped
public class RosterBean {
public String selectedCountry;
public String selectedState;
public String getSelectedCountry() {
return selectedCountry;
}
public void setSelectedCountry(String selectedCountry) {
this.selectedCountry = selectedCountry;
}
public String getSelectedState() {
return selectedState;
}
public void setSelectedState(String selectedState) {
this.selectedState = selectedState;
}
public void onCountryChange() {
System.out.println("Updating State");
}
}