AbelTrocha
AbelTrocha

Reputation: 3

jsf inputText don't execute change listener event

i have two .xhtml pages with two similar inputText elements ("customer id" and "customer name"), in both pages the inputText for "customer id" launch a request ajax event to looking for the name of the customer and to put it in the inputText for "customer name". when i run the web application and use the first pages everything work fine, but if i don't use the first page and go to the second page (via commandlink in first page) without use the first page then the inputText for search the customer name don't work, indeed not enter to the testBean2.searchCustId method.

the code:

first page

<h:form>
    <h:commandLink action="page2">PAGE 2</h:commandLink><br/>
    <h:outputText value="customer id: "/>
    <h:inputText id="custoId" label="CUSTOMER_ID" value="#{testBean.custId}" required="true">
        <a4j:ajax event="change" listener="#{testBean.searchCustId(testBean.custId)}" render="custoName"/>
        <f:validateLength minimum="1" maximum="12"/>
    </h:inputText>
    <rich:message for="custoID" ajaxRendered="true"/>
    <h:outputText value="Customer name: "/>
    <h:inputText id="custoName" label="CUSTOMER_NAME" value="#{testBean.custName}"/>
    <rich:message for="custoName" ajaxRendered="true"/>
</h:form>

second page

<h:form>
    <h:outputText value="customer id: "/>
    <h:inputText id="custoId2" label="CUSTOMER_ID2" value="#{testBean2.custId}" required="true">
        <a4j:ajax event="change" listener="#{testBean2.searchCustId(testBean2.custId, 0)}" render="custoName2"/>
        <f:validateLength minimum="1" maximum="12"/>
    </h:inputText>
    <rich:message for="custoId2" ajaxRendered="true"/>
    <h:outputText value="customer name: "/>
    <h:inputText id="custoName2" label="CUSTOMER_NAME2" value="#{testBean2.custName}"/>
    <rich:message for="custoName2" ajaxRendered="true"/>
</h:form>

now jsf managed bean

testBean

public class TestBean {

    private String custId;
    private String custName;

    public TestBean() {
    }

    //getter and setter

    public void searchCustId(String ced){
        custName = ced + "- SOME CUSTOMER NAME"; //THIS IS FOR TESTING
    }
}

testBean2

public class TestBean2 {

    private String custId;
    private String custName;

    public TestBean2() {
    }

    //getter and setter

    public void searchCustId(String ced, int que){
        custName = ced + " - " + que; //THIS IS FOR TESTING
    }
}

what may be happening?

i'm new on this and my english is not good :)

Upvotes: 0

Views: 1882

Answers (1)

Kishor Prakash
Kishor Prakash

Reputation: 8171

You can use f:setPropertyActionListener.

<a4j:ajax event="change" listener="#{myBean.listener}">
  <f:setPropertyActionListener target="#{myBean.someProperty}" value="your_value_here"/>
</a4j:ajax>

But from your code it looks like you are trying to pass the very one value that's used on h:inputText, that's not necessary:

<h:inputText id="custoId" label="CUSTOMER_ID" value="#{testBean.custId}" required="true">
        <a4j:ajax event="change" listener="#{testBean.searchCustId}" render="custoName"/>
        <f:validateLength minimum="1" maximum="12"/>
</h:inputText>

After Ajax event the values testBean.searchCustId will automatically be updated on Managed bean after passing the validation, so your listener could be:

public class TestBean {

    private String custId;
    private String custName;

    public TestBean() {
    }

    //getter and setter

    public void searchCustId(){
        custName = custId + "- SOME CUSTOMER NAME"; //THIS IS FOR TESTING
    }
}

Upvotes: 1

Related Questions