p:commandLink actionListener is processed for each row of p:dataTable while rendering the page

7.9k Views Asked by At

I have a dataTable that shows data stored in a database. One of the column contains a commandLink (p:commandLink) to edit selected row which is fine.

I have a problem during the rendering of my xhtml page. It seems that the method of backingBean in the actionListener attribute of commandLink is processed for every row in the table, but the actionListener should be processed only when link is clicked.

Here is my xhtml page (part of):

<h:form id="formElenco">
    <p:dataTable id="dt1" value="#{myBean.itemList}" var="item">
        <f:facet name="header">
            <h:outputText value="header" />
        </f:facet>
        <p:column>
            <f:facet name="header">
                <h:outputText value="Name" />
            </f:facet>
            <h:outputText value="#{item.name}"/>
        </p:column>
        <p:column>
            <f:facet name="header">
                <h:outputText value="value" />
            </f:facet>
            <h:outputText value="#{item.value}"/>
        </p:column>
        <p:column>
            <p:commandLink id="lnkItemUpdate" value="Edit"
                            onstart="document.getElementById('divUpdateItem').style.display = 'block';"
                            actionListener="#{myBean.updateItemAction(item)}" update=":formUpdateItem" />
        </p:column>

    </p:dataTable>
</h:form>

<div id="divUpdateItem" style="display:none" > 
    <h:form id="formUpdateItem">
        Nome <h:inputText value="#{myBean.name}" /><br/>
        Met  <h:inputText value="#{myBean.value}" /><br/>
        <h:inputHidden value="#{myBean.id}" />
        <h:commandButton action="#{myBean.updateItemAction}" value="Save" />
    </h:form>
</div>

Here are myBean's method (myBean is requestScoped):

public String updateItemAction(Entity item){
    this.setId(item.getId());
    this.setName(item.getName());
    this.setValue(item.getValue());
    return null;
}

public String updateItemAction() {
    Entity entity = new Entity();
    entity.setId(this.getId());
    entity.setName(this.getName());
    entity.setVAlue(this.getValue());
    updateEntityQueryMethod(entity);
    return null;
}
1

There are 1 best solutions below

0
On BEST ANSWER

This isn't a valid method signature for an actionListener, so it's treated as a value expression by the <p:commandLink>.

You should be using action instead.

<p:commandLink id="lnkItemUpdate" value="Edit"
    onstart="document.getElementById('divUpdateItem').style.display = 'block';"
    action="#{myBean.updateItemAction(item)}" update=":formUpdateItem" />

Note that you can also just return void instead of a null String.

public void updateItemAction(Entity item) {
    this.setId(item.getId());
    this.setName(item.getName());
    this.setValue(item.getValue());
}

A valid actionListener method signature would be a void method taking javax.faces.event.ActionEvent argument:

public void actionListener(ActionEvent event) {
    // ...
}

See also: