How to make redirect using navigation-rule

前端 未结 1 1050
生来不讨喜
生来不讨喜 2020-12-31 15:44

A Redirect from Bean in JSF can be done by

externalContext.redirect(\"foo.xhtml\");

But I want to use the rules, set in

1条回答
  •  难免孤独
    2020-12-31 16:15

    The ExternalContext#redirect() takes an URL, not a navigation outcome.

    You need to return navigation outcomes from action methods declared to return String.

    public String submit() {
        // ...
    
        return "from-outcome";
    }
    

    You can configure the navigation case to send a redirect by adding .

    
        
            from-outcome
            /foo.xhtml
            
                
                    param
                    bar
                
            
        
    
    

    Note that you can also just make use of JSF implicit navigation without the need for this XML mess:

    public String submit() {
        // ...
    
        return "/foo.xhtml?faces-redirect=true¶m=bar";
    }
    

    If you're inside an event or ajax listener method which can't return a string outcome, then you could always grab NavigationHandler#handleNavigation() to perform the desired navigation.

    public void someEventListener(SomeEvent event) { // AjaxBehaviorEvent or ComponentSystemEvent or even just argumentless.
        // ...
    
        FacesContext context = FacesContext.getCurrentInstance();
        NavigationHandler navigationHandler = context.getApplication().getNavigationHandler();
        navigationHandler.handleNavigation(context, null, "from-outcome");
    }
    

    The non-navigation-case equivalent for that is using ExternalContext#redirect().

    0 讨论(0)
提交回复
热议问题