I'm new to JSF and I stumpled upon some behavior I can't explain.
I have a dataTable that displays a list of entities. I have an update button in each row that will lead to an update form for that entity, and I have a delete button in each row that will call an action method that will delete that entity. That works fine.
If I click a delete button, the entity will be deleted. Just like I want. But... if I hit the refresh button of my browser after I deleted an entity, it will delete another entity. (Yep, without clicking the delete button. The URL in my browser address bar is the usual http://www.example/webapp/listofentities.xhtml.)
Has anyone seen this before?
Thank you very much!
Joined: Jul 06, 2005
Its a general issue with web applications.
"Refreshing" the page will send exactly the same request again.
If your request was "delete the item at position 2 in this list" then the server will happily fulfill your request.... again and again and again.
So what can you do to stop it?
1 - Make your delete call dependent on the id of the element you are deleting. ie your request becomes "delete the item with the id '42'" If that gets refreshed, it should either be a no-op (item 42 is already deleted) or throw an error.
2 - Follow a Post-Redirect-Get pattern. After the delete option, redirect to your result page rather than forward.
In essence this creates two requests
- delete the data
- display the new list.
Then if the user hits the refresh button, all they do is repeat the "display the new list" request
That is conceptually what you have to do.
Doing it in JSF?
#1: Depends how you have written your delete code :-)
#2: From what I remember you should be able to configure a request to redirect rather than forward to its result page as part of the configuration. But then I might be mixing it up with struts.