aspose file tools*
The moose likes JSF and the fly likes scope problem with simple search page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "scope problem with simple search page" Watch "scope problem with simple search page" New topic
Author

scope problem with simple search page

Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
This seams such a simple use case and I there must be easy solution but I just do not see it myself. Maybe someone can help

I have a page with a search form and underneath it is a table that shows the results:

So very simple scenario. The table that shows the results is PrimeFaces dataTable with pagination and this is the fact that causes the problem I have.

Alternative 1:
If I put the backing bean (searchBean) in request scope then the search works fine and first page of the results shows in the table. But when I go to any other page (dataTable makes AJAX call to fetch the the results again to show that page) it stops working since the results are no longer there and the table does not call the search again to fetch the results. And on the other hand if the table WAS smart enough to call the search method I would not necessarily want that since it makes unnecessary call to DB the fetch the results again.

alternative 2:
If I put the backing bean (searchBean) in view scope then the search stops working since when I push the search button the search is made BUT right after that the searchBean is re-created and the old one is discarded. So I get no results. This would be the ideal solution if it worked since now I would have the results in a scope that stores the results as long as the user decides to stay on the page!

alternative 3:
If I put the backing bean (searchBean) in session scope then everything works BUT the user can not make simultaneous searches and I would have to make (perhaps complicated) logic to empty the unnecessary results and so on when the user no longer needs them.

So what to do? Of course I could try to mix the scopes so that part of the call (the initial search) is in request scope and part is in viewscope (going through the pages). But that solution requires two backing beans and seem like a hack and not a actual real world solution.

Any comments or help?

B Nirvan
Ranch Hand

Joined: Oct 20, 2010
Posts: 82
Ilari Moilanen wrote:
alternative 2:
If I put the backing bean (searchBean) in view scope then the search stops working since when I push the search button the search is made BUT right after that the searchBean is re-created and the old one is discarded. So I get no results. This would be the ideal solution if it worked since now I would have the results in a scope that stores the results as long as the user decides to stay on the page!

Why would the searchBean be re-created if you are on the same page ? I have successfully used ViewScoped beans for case you have described. Maybe I am not able to fully understand your point. Can you please elaborate why your bean is re-created.

regards,
Nirvan.
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
It is good to hear that it Should work. I am relatively new to JSF and so far everytime I have used ViewScoped it has only worked with ajax and not between full page loads. I may be doing something constantly wrong! Or maybe I have some setting set somewhere that prevents me getting the right result.

But this means that my problem is not what I ask in this thread but why viewscoped does not work correctly...

Can you see a problem in my code? The search method of my backing bean returns "search" and that is the name of my page (search.xhtml).
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
Well, how embarrassing. The search method should return null for the framework to realize that the view should live on.

I mark this thread as solved.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: scope problem with simple search page