• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamic navigation from Backing Bean Constructor.  RSS feed

 
Sudeep Agrawal
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am in a situation where I need to implement a redirect from the construtor of a managedBean.

For instance :
Page 1 : Has a Name Search feature. Search results navigate to Page 2.
Page 2 : Has Extra information about the name based on DB querries from the DB in the page constructor. To Load data requried to display in Page2. There are hyperlinks on each information on this page that goes to the third Page. Once the information is worked, the related information linked to the name is removed from the DB and I will navigate to Page 2.

The problem that I am not able to resolve is, in the construtor of Page 2 when the DB queries donot return any more information on the name, I need to directly go to Page 1. How can I redirect from the contructor of the second page's specific backing bean.

I tried using Navigation Handler, and setting specific rootView in context.
Netiher works when called from the constructor.

If I implement the PhaseListener, then would the pahse Listener logic be applied to all the other pages in the application ?

Please advice.
- Brain Fried.
 
Tim Holloway
Bartender
Posts: 18663
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't work that way. A backing bean is a javabean. It is accessed by the JSF framework, but it is not an active part of the framework.

The constructor is invoked when the framework detects a need for the existence of the bean. Depending on scope, this can be at application startup or on demand, or repeatedly (request scope). Construction is not part of the navigation process. It simply initializes the bean.

If, once the bean is constructed, you invoke some sort of action processor in the bean, the action processor can control the navigation at that point, but the only contact between that process and the original construction lies in what the constructor initialized things to and then only if the action process explicitly chooses a result based on that information.

If you do that, you can inject construction information in the usual way using the faces-config file, but this is all done on demand and not because of spontaneous actions.
 
Sudeep Agrawal
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got a work around by using sendRedirect("destinationPage"); in the constructor, however I am not too sure if using sendRedirect or forward is a good practice in JSF.

Anyone please advice.
 
A. Dusi
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Considering you want to have Page 2 's bean in request scope and do the DB query in the constructor, you could possibly try this:
On click of Search button on Page 1, call page2Bean's searchAction method.
action="#{page2Bean.searchAction}"

Have a boolean variable that stores the status of your DB query, lets say showResults.
In your constructor, if there is no need to show Page 1, set this boolean to false, else to true.

And the searchAction method must return a String to show appropriate page: Page 1 or Page 2, something like this:


Of course, set up navigation rules in the config file appropriately.

Actually I would prefer the searchAction method do the DB query instead of the constructor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!