Student
Student

Reputation: 28345

How to identify a postback from page refresh (F5), and not from a button press?

I'm using a <f:event type="preRenderView" listener="#{bean.listener()}"/> event. It's triggered both on page reload (F5), and when a press a button on the page (that renders a table).

                    <a4j:commandButton
                        value="rerender"
                        action="#{bean.updateTable()}"
                        render="myTable" />

I need to call the listener only on page refresh (F5), not when the button is pressed.

Is there a way?

FacesContext.getCurrentInstance().isPostback() will not solve, because the button is also a POST.

Upvotes: 1

Views: 1243

Answers (2)

David Mantilla
David Mantilla

Reputation: 248

You can't avoid the listener to be called, but inside the listener you can check that the request is not a post and is not an AJAX request.

Look this: How to differentiate Ajax requests from normal Http requests?

And also you can know the request method :

request.getMethod()

If it's not an ajax and it's a GET, then the page has been requested by the first time, or refreshed with F5.

Upvotes: 1

TheCoolah
TheCoolah

Reputation: 529

I'm not sure if you can entirely avoid calling the listener. I think it's part of the JSF spec.

You could work around it though. Add an event to the button click (onclick I guess) that sets a hidden variable buttonClicked. In your listener, check for the variable. If it's set, return immediately. The listener code should still run on a normal refresh but not when you click the button.

Upvotes: 0

Related Questions