2012-08-03 88 views
1

我有包含testcomponent1的主頁,其中inturn包含testcomponent2。 Ajax調用在testcomponent2中。該調用是在JavaScript中發起的,但從未擊中控制器。如果ajax調用被移入testcomponent1,它將觸發該調用並將其發送給控制器。我將包含測試代碼供您參考。JSF 2:Ajax未在嵌套複合組件中觸發

的TestController:

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; 
} 
} 

testPage.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> 

感謝

回答

1

我認爲這個問題是在<h:panelGrid id="#{cc.clientId}"...>。默認情況下,複合組件是UINamingContainer的一個實例,因此可以使用任何名稱來設置一個id並使用它。一個好的做法是避免ID字段中的EL表達式。

0

您不應該重新指定#{cc.clientId}作爲JSF組件的ID,而是作爲普通香草HTML元素的ID,如<span><div>

<span id="#{cc.clientId}"> 

<div id="#{cc.clientId}">