Marwa Dosoky
Marwa Dosoky

Reputation: 416

Change commandLink Id by my code in datatable

I need a way to manage the creation of the commandlink ID in DataTable, the problem is that when i use it for deleting a record and interrupt the rendering of the page (reloading it by 'ctrl+f5' ) it assigns the same id to another link button, which results in deleting the row containing it.

Upvotes: 1

Views: 98

Answers (1)

BalusC
BalusC

Reputation: 1108722

The problem in question is sound, but the requested solution is not the right one and not easily to achieve in JSF — basically, you'd need to homebrew a custom command link renderer which is designed specifically for usage in data tables and is able to recognize the specific entity.

The right solution is send a redirect to the same view after POST.

public String delete() {
    // ...
    return FacesContext.getCurrentInstance().getViewRoot().getViewId() + "?faces-redirect=true"; // Feel free to hardcode the view ID, though.
}

(if you intend to display some faces message along it, use the flash scope)

A browser refresh would then result in only the redirect being refreshed rather than the POST action.

An alternative is to submit by ajax instead.

<h:commandLink ...>
    <f:ajax execute="@form" render="@form" />
</h:commandLink>

A browser refresh would then only re-execute the last synchronous request, which would be the initial GET request which opened the page in question.

Upvotes: 1

Related Questions