jsfprimefacesdatatablevaluechangelistener

valueChangeListener calls for every datatable column instead of only changed column


I am learning JSF and have an understanding issue the way valueChangeListener works. I am trying to use the it inside the dataTable. The objective of using valueChangeListener here is- I want to track the changes user performs in the Title and First Name column.

Please find below code :

<p:dataTable var="tempVar"
                        value="#{tempView.tempVO}">
                        <p:column>
                            <h:outputLabel value="Academic Title:" />
                            <p:inputText value="#{tempVar.title}"
                                style="margin-left:10px;margin-top:20px;width:140px;height:25px"
                                valueChangeListener="#{tempView.titleChangeListener}">
                                <f:attribute name="TITLE" value="TITLE" />
                            </p:inputText>

                            <br />


                            <h:outputLabel value="First Name:" />
                            <p:inputText value="#{tempVar.firstName}"
                                style="margin-left:35px;margin-top:20px;width:140px;height:25px"
                                valueChangeListener="#{tempView.firstNameChangeListener}">
                                <f:attribute name="FIRST_NAME"
                                    value="FIRST_NAME" />
                            </p:inputText>
                 <p:column>
    </p:dataTable>

Bean Code

@PostConstruct
    public void init() {

        try {
            tempVO = tempService
                    .fetchDataFromDatabase(tmpDataBean
                            .tempId());

            System.out.println("success");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public void titleChangeListener(ValueChangeEvent event) {

        String title = (String) ((UIInput) event.getSource()).getAttributes()
                .get("TITLE");

        System.out.println(title);

    }

    public void firstNameChangeListener(ValueChangeEvent event) {

        String firstName = (String) ((UIInput) event.getSource())
                .getAttributes().get("FIRST_NAME");
        System.out.println(firstName);
    }

The issues I am facing is- valueChangeListener is called for all the database columns. For instance if I am changing only Title, it is calling first name valueChangeListener also. The answer provided by @BaluC on this links says that it should be called only when value changed. [When to use valueChangeListener or f:ajax listener?

[1]: When to use valueChangeListener or f:ajax listener? Could someone please help to understand the way valueChangeListener works, Am I using it in the wrong way? Thank you.


Solution

  • Finally able to solve the issue. valueChangeListener is working the way it is expected, the issue was with the data in the list. if data is null for some of the columns and if you submit the form, null got converted into empty string internally and valueChangeListener consider it as a change,hence method got called.