Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Navigating to a screen from the backing bean method  RSS feed

 
Ganesh Podaralla
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I defined @PostConstruct method in my backing bean. This method will invoke the business method to get the data from the database so that I can display it on the page. Sometimes, I have to navigate to a different screen based on some code I receive by calling the business method. All the navigation rules are defined in the faces-config.xml. Is there any way I can forward/redirect the request to a different page from the PostConstruct backing bean method itself based on the action-outcomes defined in the navigation rule.

For example, if I am enter some details on home.xhtml and on click of some button on the page, the @PostConstruct method will call the business method to get the details. Since the return type of PostConstruct is void, how can I navigate to a different screen based on the output codes returned from the business methods. I have different pages defined in the navigation rules for different output codes from the business methods. But not sure, how I can forward/redirect the request to the page from the backing bean.

Any help is appreciated.

Thanks,
Ganesh
 
ckaps kapsi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use this in your backing bean method

 
Ganesh Podaralla
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply. I do not want to use page location for forwarding/redirecting the request. I want to pass navigation-rule from-outcome somehow in my backing bean postconstruct method to pass to the other page.

I hope I make sense.

Thanks,
Ganesh
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@PostConstruct is not a UI function. It's called after a bean is constructed, exactly once, just like the constructor is. Beans may constructed when a View references them or they may also be constructed in advance using manual processes.

Navigation is done as a consequence of an Action method. What you want sound more like you want to avoid a View based on construction results, not as a result of processing data on the View. That means that you're going to have to do one or more of the following:

1. Alert the View that would invoke this View not to do so.

2. Define the View in such a way as to cause itself to refuse to render.

First of all, you probably should add a "don't-render" property to the backing bean in question and set it true/false based on the results of the @PostConstruct.

Solution #1 is simple. Just provide an alternate Action result on the Action that would otherwise bring up the View in question, and have that Action result navigate to an alternate page.

Solution #2 can vary. One way is to place a "redirect" in the View itself, conditionally fired based on the "don't-render" property in the backing bean. Another way is to render the view, but to censor the parts you don't want to render using the "rendered" attribute on a containing element.

There are other alternatives, such as redirecting in a Servler Filter or Phase Listener, but these are the simplest ways I can come up with at this hour of the morning. And in JSF, simpler is almost always better. Too many people out there are making JSF more complicated than it needs to be for what they'd doing.
 
Ganesh Podaralla
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The below was the code I was looking for. Thanks for all your responses. Using the below code, you can navigate to the page from the backing bean using the navigation rules you defined.

FacesContext.getCurrentInstance().getApplication().getNavigationHandler()
.handleNavigation(FacesContext.getCurrentInstance(), "from-action",
"from-outcome");
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!