8

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

1 Answers1

20

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Every time I see an answer like this I feel like there is some big section of "the manual" that I should have read but didn't. @BalusC do you happen to have a link to all the rules that apply to the return value of the **action** method? This is the first I have heard of the **faces-redirect** parameter. What other parameters are possible there? I suspect there is something that would have been useful that I don't know about. – AlanObject Oct 21 '11 at 14:43
  • 4
    @Alan: This particular one (PRG pattern) is general web development knowledge and understanding of how HTTP and webbrowsers works. As to the supported special action parameters, the other one is `includeViewParams=true`. This includes all declared `` of current view as request parameters of the outcome target. I learnt it from reading blogs/documentation/resources. It's also mentioned in Java EE 6 tutorial. It should also be mentioned in a bit decent JSF 2.0 book, like [JSF 2.0: The Complete Reference](http://jsfcompref.com/) and [Core JSF](http://horstmann.com/corejsf/). – BalusC Oct 21 '11 at 14:53
  • @BalusC I have the same issue, only I have a datatable where I can do CRUD on rows. When I refresh page, last action is resubmitted again. How can I solve it?I do not have a submit button? – gmode Sep 30 '15 at 13:05
  • As answered, use PRG or ajax. – BalusC Sep 30 '15 at 13:15
  • Could you please elaborate? How would I bind ajax to reloading? – gmode Sep 30 '15 at 13:33
  • No idea. You didn't ask a question with your problem in MCVE flavor. – BalusC Sep 30 '15 at 13:34