3

I have a search result page with a "Show more results" button. The backing bean is session-scoped and we use a preRenderView to execute the search method in the backing bean:

<f:event type="preRenderView" listener="#{SearchBean.searchSolutions}" />

The "Show more results" button is defined like this:

<h:commandButton action="#{SearchBean.onClickShowMoreResults()}">
    <f:ajax disabled="false" render=":searchResultsForm"/>
</h:commandButton>

Here is the problem I have and the steps to reproduce :

  1. Execute a search.
  2. Click on a search result that leads to a result page.
  3. Use the browser back button.
  4. Click on the "Show more results" button.
  5. Problem --> the onClickShowMoreResults() function is not called and the searchSolutions() listener method is called instead (this happens intermittently, most of the time the function is called correctly and everything is alright altought once I get the problem I can reproduce it every time with steps 2 to 4 without starting a new search).

I tried skipping the ajax requests in the listener method (as explained here) and it solves the problem, but this is not possible for me because there are ajax requests that need to execute the listener method (changing search criteras).

Is there something I don't understand about the preRenderView or is there another way to achieve what I am trying to do ?

Thanks for the help!

Community
  • 1
  • 1
Simon
  • 1,605
  • 13
  • 22
  • Did you look in the ajax response body? Did you see a `ViewExpiredException` in there? – BalusC Sep 03 '13 at 15:37
  • @BalusC I get no exception thrown in the console. How do I access the ajax response body ? – Simon Sep 03 '13 at 15:44
  • Just look in browser's builtin HTTP traffic monitor. Press F12 in Chrome/Firefox(Firebug)/IE>=9 to open web developer tools and then look in "Net" or "Network" tab. – BalusC Sep 03 '13 at 15:45
  • @BalusC thanks for the tip, but I see no exception in there. Also, I realized that once I get the problem, I can reproduce it every time I do steps 2->4 without starting a new search. – Simon Sep 03 '13 at 15:51
  • Maybe [this](http://stackoverflow.com/questions/8062267/browser-back-viewscope-beans) can help you – danRod Sep 03 '13 at 22:33
  • @danRod thanks for the link, but my view is Session scoped and I cannot change this..! – Simon Sep 04 '13 at 15:12
  • Did you try it? I tested with a session scoped bean and it worked...atleast with jsf2.0 – danRod Sep 06 '13 at 14:44

1 Answers1

0

Since I got no answer, I found a workaround this problem.

I made sure to have no ajax calls to the listener method (doing some sacrifices) and used BalusC's answer in this thread.

Using :

if (FacesContext.getCurrentInstance().getPartialViewContext().isAjaxRequest()) { 
       return; // Skip ajax requests.
}

in the listener method solves this problem.

Community
  • 1
  • 1
Simon
  • 1,605
  • 13
  • 22
  • Sure, but you said that this happens intermittently which made the question almost unanswerable with the above answer as that does really not fix the "intermittently" part. The `preRenderView` is supposed to be **always** invoked on *every* single HTTP request. Or is that "intermittently" after all just a misobservation? – BalusC Sep 04 '13 at 17:00
  • It does happen intermittently (the problem happens sometimes after 5 tries, sometimes more, otherwise it works well)... And this solution blocks the new search done in the listener method. The thing I don't understand is why the "Show more" action is still done even if it doesn't go into the method. – Simon Sep 04 '13 at 17:07
  • What I forgot to tell is that we have a boolean condition in the listener method that skips the search when we need to (which maybe is a bad way to do this but that's what I'm working with..). And when the problem happens, since it did not go into the ShowMore method, the boolean is true and all the code in the listener method is executed. – Simon Sep 04 '13 at 17:09