我的主页包含 testcomponent1,而 testcomponent1 又包含 testcomponent2。Ajax 调用在 testcomponent2 中。调用是在 javascript 中发起的,但从未到达控制器。如果将 ajax 调用移到 testcomponent1 中,它会触发调用并将其发送到控制器。我包括测试代码供您参考。
测试控制器:
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.AjaxBehaviorEvent;
@ManagedBean
@ViewScoped
public class TestController implements Serializable {
/** <code>serialVersionUID</code> */
private static final long serialVersionUID = -999213365169849345L;
private int customerkey;
private String customerName;
public void processKeyChange(AjaxBehaviorEvent e){
System.out.println("I am in the Ajax method and my value is: " + customerkey);
}
//GETTERS AND SETTERS
public int getCustomerkey() {
return customerkey;
}
public void setCustomerkey(int customerkey) {
this.customerkey = customerkey;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
}
测试页.xhtml
<ui:define name="searchCriteria">
<h:form id="orderHeaderForm" prependId="false">
<h:panelGroup id="orderDiv" layout="block">
<com:testcomponent1 id="testComponent1"
customerKey="#{testController.customerkey}"
customerName="#{testController.customerName}"/>
</h:panelGroup>
</h:form>
</ui:define>
</ui:composition>
testcomponent1.xhtml
<cc:interface>
<cc:attribute name="customerKey" type="java.lang.Integer" />
<cc:attribute name="customerName" type="java.lang.String" />
</cc:interface>
<cc:implementation>
<h:panelGroup id="#{cc.clientId}" layout="block">
<com:testcomponent2 id="testcomponent2"
key="#{cc.attrs.customerKey}"
name="#{cc.attrs.customerName}" />
</h:panelGroup>
</cc:implementation>
testcomponent2.xhtml
<cc:implementation>
<h:panelGrid id="#{cc.clientId}" columns="2" border="0" cellspacing="0" cellpadding="0">
<h:outputLabel id="customerid" for="txtcustomerKey"/>
<h:inputText id="txtcustomerKey" value="#{cc.attrs.key}" onchange="alert (document.getElementById(this.id).value)">
<f:ajax event="change" listener="#{testController.processKeyChange}" execute="@this" render="txtCustomerName" />
</h:inputText>
<h:outputLabel id="customerName" for="txtCustomerName"/>
<h:outputText value="#{cc.attrs.name}" id="txtCustomerName" />
</h:panelGrid>
</cc:implementation>
谢谢