jQuery in Action, 2nd edition*
The moose likes JSF and the fly likes dataTable: action executes on page refresh Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "dataTable: action executes on page refresh" Watch "dataTable: action executes on page refresh" New topic
Author

dataTable: action executes on page refresh

Paolo Pinkel
Ranch Hand

Joined: May 04, 2011
Posts: 35
Hey!

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!
P.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dataTable: action executes on page refresh
 
Similar Threads
problem for update and delete row in table using jsp
Problem with datatable update
Delete an empty row from datatable
Refreshing a Page, repeats last action
How to delete the persisted entity in EJB3.0