1

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.

The Student
  • 27,520
  • 68
  • 161
  • 264

2 Answers2

1

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.

Community
  • 1
  • 1
David Mantilla
  • 228
  • 1
  • 10
  • Maybe also refer to this: http://stackoverflow.com/questions/14687910/how-to-avoid-prerenderview-method-call-in-ajax-request (actually current question is the reverse of the one I refer to here, only for newer versions of jsf) – Kukeltje Apr 21 '17 at 08:40
0

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.

TheCoolah
  • 519
  • 1
  • 4
  • 16