Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid re-execution of last form submit action when the page is refreshed?

Tags:

jsf-2

I am working on project which is developed in JSF. Whenever we are refreshing the JSF page, then the last action event is re-executed. For example, when I submit the form to delete an entry of a list and refresh the result page, then another entry from the list at the same position is deleted as well. How is this caused and how can I solve it?

i have tried in faces-config.xml but that does not solve my problem,

To get more clear on Problem i am facing is that i am commandLink to remove one resource from datatable ,i am using actionlistener attribute which calls one method in my backingbean,so problem is when ever i am refreshing the page action event getting occured and method is executed which remove another resource from the table . Thanks in advance

like image 982
Mohaammed Shareef Avatar asked Oct 21 '11 14:10

Mohaammed Shareef


1 Answers

The symptoms indicate that the page was requested by a POST request and that you're ignoring the webbrowser's warning that the data will be resent when refreshing the request. Refreshing a POST request will of course result in it being re-executed. This is not a JSF specific problem.

The common solution to that is to send a redirect to a GET request after executing the POST request. This way the client will end up having the GET request in the browser view. Refreshing this will then only re-execute the GET request which doesn't (shouldn't) modify anything (unless you're doing this in the constructor of a request scoped bean associated with the view). This is also known as the POST-Redirect-GET pattern.

With JSF 2.0, you can achieve this by simply adding faces-redirect=true parameter to the bean action's outcome.

public String submit() {
    // ...

    return "viewid?faces-redirect=true";
}

If you're still using old fashioned <navigation-case>s in faces-config.xml, then the same effect can be achieved by adding <redirect/> to the case.

The only disadvantage is that request scoped beans are garbaged this way (a redirect basically instructs the webbrowser to create a brand new request) and thus you cannot pass data in the request scope in order to redisplay it in the redirected page. For example, displaying a success message. In JSF 2.0 you could instead use the flash scope for this or to just let the POST take place by <f:ajax> submit instead of a normal submit.

like image 200
BalusC Avatar answered Oct 02 '22 02:10

BalusC