Navigation Examples in JSF

Here we go..

The value of the "action" attribute is called the "outcome".

An outcome can be mapped to a "view ID".

If no mapping exist, outcome is transformed into a view ID following the steps:
-> If outcome has no file extension, append the extension of the current view.
-> If outcome does not start with "/" prepend the path of the current view.

Example 1
<h:form>
    <h:commandButton action="goodbye" value="Go to goodbye!"/>
</h:form>
 
<h:form>
    <h:commandButton action="goodbye?faces-redirect=true" value="Go to goodbye!"/>
</h:form>
Example 2
<h:form>
    <h:commandButton action="#{blahBlah.verifyUser()}" value="Go to goodbye!"/>
</h:form>
public String verifyUser(){
    if (false) {
        return "hello?faces-redirect=true";
    }
    return "goodbye?faces-redirect=true";
}
Example 3
<h:form>
    <h:commandButton action="#{blahBlah.verifyUser('tugay')}" value="Go to goodbye!"/>
</h:form>
@ManagedBean
@SessionScoped
public class BlahBlah {
    public String verifyUser(String name){
        if (!name.equals("koray")) {
            return "hello?faces-redirect=true";
        }
        return "goodbye?faces-redirect=true";
    }
}
Example 4
<h:form>
    <h:commandButton action="#{blahBlah.verifyUser('koray')}" value="Go to goodbye!"/>
</h:form>
@ManagedBean
@SessionScoped
public class BlahBlah {
    public String verifyUser(String name){
        // Redirect goes to faces-config.xml not here!
        if (!name.equals("koray")) {
            return "fail";
        }
        return "success";
    }
}
<navigation-rule>
    <from-view-id>/hello.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>success</from-outcome>
        <to-view-id>/goodbye.xhtml</to-view-id>
        <redirect />
    </navigation-case>
    <navigation-case>
        <from-outcome>fail</from-outcome>
        <to-view-id>/hello.xhtml</to-view-id>
        <redirect />
    </navigation-case>
</navigation-rule>
Example 5
<!-- hello.xhtml -->
#{flash.message}
<br />
<h:form>
    <h:inputText id="username" value="#{blahBlah.username}" />
    <br />
    <h:commandButton action="#{blahBlah.verifyUser()}" value="Go to goodbye!"/>
</h:form>
 
<!-- goodbye.xhtml -->
<f:view>
    <h:body>
        #{flash.message}
    </h:body>
</f:view>
@ManagedBean
@SessionScoped
public class BlahBlah {
    private String username;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String verifyUser(){
        final Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
        if (!username.equals("koray")) {
            flash.put("message", "Your username is not Koray!");
            return "fail";
        }
        flash.put("message", "Your username is Koray!");
        return "success";
    }
}
<navigation-rule>
    <from-view-id>/hello.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>success</from-outcome>
        <to-view-id>/goodbye.xhtml</to-view-id>
        <redirect />
    </navigation-case>
    <navigation-case>
        <from-outcome>fail</from-outcome>
        <to-view-id>/hello.xhtml</to-view-id>
        <redirect />
    </navigation-case>
</navigation-rule>
Example 6
<!-- hello.xhtml -->
#{param.msg}
<br />
<h:form>
    <h:inputText id="username" value="#{blahBlah.username}" />
    <br />
    <h:commandButton action="#{blahBlah.verifyUser()}" value="Go to goodbye!"/>
</h:form>
 
<!-- goodbye.xhtml -->
<f:view>
    <h:body>
        #{param.msg}
    </h:body>
</f:view>
@ManagedBean
@SessionScoped
public class BlahBlah {
    private String username;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String verifyUser(){
        if (!username.equals("koray")) {
            return "hello?faces-redirect=true&msg=" + username;
        }
        return "goodbye?faces-redirect=true&msg=" + username;
    }
}
Example 7
<!-- hello.xhtml -->
#{param.msg}
<br />
<h:form>
    <h:inputText id="username" value="#{blahBlah.username}" />
    <br />
    <h:commandButton action="#{blahBlah.verifyUser()}" value="Go to goodbye!"/>
</h:form>
 
<!-- goodbye.xhtml -->
<f:view>
    <h:body>
        #{param.msg}
    </h:body>
</f:view>
@ManagedBean
@RequestScoped
public class BlahBlah {
    private String username;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String verifyUser(){
        if (!username.equals("koray")) {
            return "fail";
        }
        return "success";
    }
}
<navigation-rule>
    <from-view-id>/hello.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>success</from-outcome>
        <to-view-id>/goodbye.xhtml</to-view-id>
        <redirect include-view-params="true">
            <view-param>
                <name>msg</name>
                <value>#{blahBlah.username}</value>
            </view-param>
        </redirect>
    </navigation-case>
    <navigation-case>
        <from-outcome>fail</from-outcome>
        <to-view-id>/hello.xhtml</to-view-id>
        <redirect include-view-params="true">
            <view-param>
                <name>msg</name>
                <value>#{blahBlah.username}</value>
            </view-param>
        </redirect>
    </navigation-case>
</navigation-rule>