Page Navigation

Types of Navigation

There are different ways of navigating from one page to another:

  • HTTP GET by clicking on a non-AJAX menu item, a non-AJAX link or a non-AJAX button
  • AJAX call (HTTP POST) with replacing the current browser content with the content of a different page (also known as PAGE FORWARD), the URL in the browser address bar remains the same
  • AJAX call (HTTP POST) with a redirect and thus a subsequent HTTP GET (also known as POST/REDIRECT/GET PATTERN), the URL in the browser address bar changes

Ways of specifying Navigation Targets

These are the ways you can specify the URLs for pages that should be navigated to.

Causing UI Component Page Identifier Navigation Type
<h:outputLink> the value attribute’s value HTTP GET
<p:menuitem>
with url or outcome set
the attribute’s value HTTP GET
<p:menuitem>
with ajax=false
return value in related action handler method (attribute action) HTTP GET
<p:menuitem>
with ajax=true
return value in related action handler method (attribute action) AJAX call
page forward or post/redirect/get (when ?faces-redirect=true is appended)
<p:commandButton>
<p:commandLink>
return value in related action handler method (attribute action) AJAX call
page forward or post/redirect/get (when ?faces-redirect=true is appended)
<p:commandButton>
<p:commandLink>
with ajax=false
return value in related action handler method (attribute action) HTTP GET
<p:button>
<p:link>
the outcome attribute’s value HTTP GET
Any AJAX event handler with navHandler.handleNavigation() whatever is coded in Java AJAX call
page forward or post/redirect/get (when ?faces-redirect=true is appended)
Any AJAX event handler with externalContext.redirect() whatever is coded in Java AJAX call
post/redirect/get

Note

In the cases where an AJAX call is being made, the value of the page identifier is determined when the UI component is clicked. In the other cases the value of the page identifier is determined when the page (or part of it) is rendered.

For more information on event handling please have a look here.

Customizable Navigation

Navigation can be made customizable (which means overwritable in web application projects). You do this by letting a page identifier being taken from the generated [Layout‑Name]BeanNavigationCustomization.java. Therein you find methods for all of a page’s possible navigation cases. In the init() method of that class you define a page’s default navigation logic like this:

public void init() {
    super.init();
    this.actionNavigationSpeichernSaveToolbar = "/process-complete.xhtml";
}

The return values of the methods in [Layout‑Name]BeanNavigationCustomization.java can be overwritten by returning non-null values from the methods in App[Layout‑Name]BeanNavigationCustomization.java in the web application project.

public String getActionNavigationSpeichernSaveToolbar() {
    return "/custom-process-complete.xhtml";
}

Passing Data to other Pages

In principle there are the following ways to pass data from one page to another:

  • Storing data in a @SessionScoped bean that is accessible by the @ViewScoped bean of the following page.
  • Passing data in form of query parameters, e.g. ?user-id=5&organization-id=77
  • Passing data by means of the so-called “Flash Scope”

The first two options are more or less self-explanatory. For the Flash Scope you can have a look at the following example.

Current page, e.g. in an event handler:

...
Flash flash = facesContext.getExternalContext().getFlash();
flash.setKeepMessages(true);   // data survives POST/REDIRECT/GET with this
UserDto userDto = new UserDto();
userDto.setUserId(5);
userDto.setOrganizationId(77);
flash.put("userDto", userDto);
...

Following page, e.g. in the initApplication() method

public String initApplication() {
    ...
    Flash flash = facesContext.getExternalContext().getFlash();
    UserDto userDto = flash.get("userDto");
    ...
}

At the time being there is not yet any generation support available for the Flash Scope. All this has to be coded manually.

Note

It is possible to directly access the flash within EL expressions: value="#{flash.userDto.userId}".