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.
Asked
Active
Viewed 96 times
1 Answers
1
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.
-
it was great and solved the problem , but i might need alot of work to adapt my code to that way, is there is any way i can clear the request header, or whatever carries action information ? – Marwa Dosoky Apr 10 '13 at 10:56
-
You're welcome. No, you can't clear the already sent request. You can only tell the client to send a new and idempotent request. This is basically what the redirect does as already answered. There's no magic bullet. Just bite the bullet and fix the current application where applicable and profit from the basic web development lessons learnt for future web projects. Do note that this issue is not specific to JSF, but to web development in general. – BalusC Apr 10 '13 at 10:59