Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to programmatically handle navigation from backing bean in jsf2

Tags:

jsf-2

I want to be able to do semething in the likes of:

 @ManagedBean
 class MyBackingBean {
     public void processRequest() {
         String viewName;
         if (condition1) 
             viewName = "page1";
         else if (condition2) 
             viewName = "pagexx";     

         invokeAndRenderXHTML(viewName);
     }
 }

thanks

like image 842
demonz demonz Avatar asked Dec 05 '25 20:12

demonz demonz


2 Answers

Just in case anyone stumbles upon this old question: you can programatically invoke navigation handler like this

FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation(FacesContext.getCurrentInstance(), null, "YOUR_NAVIGATION_CASE_DEFINED_IN_FACES_CONFIG");
like image 160
yntelectual Avatar answered Dec 10 '25 00:12

yntelectual


JSF provides programmatic navigation by default. You do NOT need a third party library to effect navigation. To use JSF navigation, your method should simply return the name of the view you're trying to access and it'll navigate to that page. You could also include an optional redirect parameter to the return value to instruct the JSF context to redirect the response in full to the destination view. For your needs, just change processRequest to

   public String processRequest() {
     // String viewName unnecessary
     if (condition1) {
        return "page1";
              }
     else if (condition2) {
        return = "pagexx";  
           }
        return null;   
    // invokeAndRenderXHTML(viewName) becomes unnecessary
    }

If you choose to have the redirect option like I indicated above just change the return String to

    return "page1?faces-redirect=true"

the faces-redirect=true is the parameter that does the redirect magic

like image 37
kolossus Avatar answered Dec 09 '25 22:12

kolossus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!