1

我的主页包含 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>

谢谢

4

2 回答 2

1

我认为问题出在<h:panelGrid id="#{cc.clientId}"...>. 默认情况下,复合组件是 UINamingContainer 的一个实例,因此您可以设置一个任意名称的 id 并使用它。一个好的做法是避免在 id 字段中使用 EL 表达式。

于 2012-08-03T18:11:12.307 回答
0

您不应将其重新分配#{cc.clientId}为 JSF 组件的 ID,而应作为普通 HTML 元素的 ID,例如<span>or <div>

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

或者

<div id="#{cc.clientId}">
于 2012-08-03T19:58:03.053 回答