types of Page Navigation in JSF

types of Page Navigation in JSF

In JavaServer Faces (JSF), there are several ways to handle page navigation within a web application. Each approach provides flexibility in managing how users move between different views or pages. Here’s an explanation followed by a Java example for each type of page navigation:

types of Page Navigation in JSF

Explanation

  • 1.  Implicit Navigation:
    • Implicit navigation is the simplest form where JSF automatically determines the next view to render based on the outcome of an action method.
    • It relies on default navigation rules defined in faces-config.xml or annotations (@ManagedBean, @ManagedBean, @Named) in newer versions of JSF.
  • 2.  Explicit Navigation:
    • Explicit navigation allows developers to define navigation rules explicitly in faces-config.xml.
    • It provides fine-grained control over which view to render based on specific outcomes of action methods or conditions.
  • 3.  Programmatic Navigation:
    • Programmatic navigation involves using Java code to navigate between views based on business logic or user interactions.
    • It allows dynamic determination of the next view to render, enabling complex navigation scenarios.

Java Example

Here’s an example demonstrating each type of page navigation in JSF:

Implicit Navigation
java
package com.example.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class NavigationBean {

    public String navigateToHome() {
        // Business logic
        return "home"; // Implicit navigation outcome
    }
}

Explicit Navigation
xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                                  http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
              version="2.2">

    <navigation-rule>
        <from-view-id>/login.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>success</from-outcome>
            <to-view-id>/home.xhtml</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-outcome>failure</from-outcome>
            <to-view-id>/error.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>

</faces-config>

Programmatic Navigation
java
package com.example.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import java.io.IOException;

@ManagedBean
@RequestScoped
public class ProgrammaticNavigationBean {

    public void redirectToHome() throws IOException {
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
        ec.redirect(ec.getRequestContextPath() + "/home.xhtml");
    }
}

Explanation of Example

  • Implicit Navigation:  In NavigationBean, the navigateToHome() method returns “home”, which is interpreted by JSF to navigate to home.xhtml based on implicit rules.
  • Explicit Navigation:  In faces-config.xml, navigation rules specify that upon “success” outcome from /login.xhtml, navigate to /home.xhtml; upon “failure”, navigate to /error.xhtml.
  • Programmatic Navigation:  In ProgrammaticNavigationBean, redirectToHome() method uses ExternalContext to redirect to home.xhtml programmatically, allowing for dynamic navigation based on conditions or business logic.

These different types of page navigation in JSF provide developers with flexibility in managing user interactions and application flow, ensuring smooth transitions between views and enhancing overall user experience.