JSF/PrimeFaces selectOneMenu change view-id

前端 未结 3 1042
萌比男神i
萌比男神i 2021-02-03 16:05

I\'m using JSF2 and PrimeFaces3. How can I write selectOneMenu that would invoke JSF navigation to redirect user to another page when he change option in menu?

3条回答
  •  名媛妹妹
    2021-02-03 16:26

    Attach an ajax listener and let it navigate by NavigationHandler.

    E.g.

    
        
            
            
            
            
            
        
    
    

    (the above example expects page1.xhtml, page2.xhtml and page3.xhtml in the same context; you can even make it a instead)

    with

    private String outcome;
    
    public void navigate() {
        FacesContext context = FacesContext.getCurrentInstance();
        NavigationHandler navigationHandler = context.getApplication().getNavigationHandler();
        navigationHandler.handleNavigation(context, null, outcome + "?faces-redirect=true");
    }
    

    The ?faces-redirect=true is not necessary, but it effectively sends a redirect so that the URL in browser address bar will properly change which is better for user experience and bookmarkability of the pages.

提交回复
热议问题