Java Server Faces Page Navigation Rule

Java Server Faces Page Navigation Rule

A navigation rule in a faces-config.xml file.In our application, we create the pages directory that includes: index.jsp, Mirek.jsp, and Bolek.jsp. So the directory tree look in the following way:

/pages/

index.jsp

Mirek.jsp

Bolek.jsp

The index.jsp file is default: when we start our web application it will be displayed without our action. However, our application can have more web pages to display. If we need to navigate to other page, just use this way:

<?xml version=’1.0′ encoding=’UTF-8′?>
<faces-config version=”2.2″
    xmlns=”http://xmlns.jcp.org/xml/ns/javaee&#8221;
    xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
    xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd”&gt;

  

<navigation-rule>

    <from-view-id>/pages/index.jsp</from-view-id>

    <navigation-case>

        <from-outcome>mirek</from-outcome>

        <to-view-id>/pages/Mirek.jsp</to-view-id>

<redirect/>

    </navigation-case>

   

     <navigation-case>

        <from-outcome>bolek</from-outcome>

        <to-view-id>/pages/Bolek.jsp</to-view-id>

<redirect/>

    </navigation-case>

</navigation-rule>
    
</faces-config>

We define a navigation rules in Java Server Faces between <navigation-rule> and </navigation-rule>.

We have three pages: index.jsp, Mirek.jsp, and Bolek.jsp. To navigate from index.jsp to Mirek.jsp, we use the first snippet between <navigation-case></navigation-case>. To navigate from index.jsp to Bolek.jso, we use the second snipped between <navigation-case></navigation-case>.

<redirect/> – it helps to redirect to other page, so better do not forget this tag; after redirecting you see only the content of the redirected page, not the one of the redirected page and the one of the first page, because forward dispatching is default. In other words, redirecting between two pages is going smoothly with this tag

<from-outcome></from-outcome> – the name between these tags you can use for redirecting; it represent the page, for example:

<h:commandButton … action=”mirek” > – w use mirek as a reference to the Mirek.jsp page on other page

<from-view-id></from-view-id> – the page we go from to other page

<to-view-id></to-view-id> – the page we go to from other page

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s