Subscribe via Feed

Using Navigation Rules in Your XPages Application

Jeremy Hodge, Aug 8, 2010 12:36:32 PM

Another feature of JSF that has been surfaced in XPages is the ability to create "Navigation Rules" for your pages & application.

Navigation rules are a simple mapping of an identifier string to another XPage within your application, simplifying navigation.

For example, you can create a button that performs an action, and then based on that action, you can return the navigation rule's identifier to redirect your user to that page.

Let's look at an example. Let's take an application where we have two pages, one named "NavigationSample.xsp" and one named "default.xsp" ... Let's put a button on NavigationSample.xsp, and in the server side "onclick" event, enter the following code:

return "goHome"

Now, add a navigation rule to the NavigationSampe xpage by goin gto the all properties tab of the xpage, and clicking the + next to navigationRules. Set the outcome to goHome, and the viewId to default.xsp, like this:

The source code for your NavigationSample.xsp XPage should look like this:




    
        
        

    


    
                    refreshMode="complete">
            
        

    


Add a simple XPage called default.xsp and just type "You're home" in the page and save it. Now load the NavigationSample.xsp page in your browser, and click the button. You get redirected to default.xsp !

The real power of Navigation rules is in the ability to compute the page you want to redirect to. For example, a more complicated SSJS script for the button might be:

if (someFieldValue == "a") {
      return "goHome";
} else if (someFieldValue == "b") {
      return "goContactPage";
} else if (somFieldValue == "c") {
      return "goLogin"
} else
      return "goInvalidOption";

Each one of those return values are an identification string for a navigation rule, making life much easier.

Now, if you have lots of actions or links that link to a few of the same pages, you can see where this can help you create maintainable, clean, and readable code and Navigation for your application, and while that is a huge help, if you have to maintain copies of your navigation rules in every XPage, it can still be sort of hairy. Wouldn't it be nice to maintain a set of Navigation Rules application-wide?? Well, you can.

You can add navigation rules to your faces-config.xml file to set up global and per page navigation rules!

Let's do that now.  Modify the NavigationSample.xsp and delete the navigation rules we created previously.

Now, open your faces-config.xml file (You can read how to do that in this post) and add the following:

 
    *
   
      goHome
      default.xsp
   
 
 

Save both the faces-config and your NavigationSample and try it again ... GLOBAL-THERMO-NUCULEAR-NAVIGATION!

Now, there are many different ways you can set up Navigation rules. You can specify rules for specific pages by putting the page's name to which it applies in the node, like NavigationSample.xsp, which will restrict everything in that navigation rule to only apply to that page. You can also use wild cards, like the sample above, where '*' applies the rule to every page. For more samples and use-cases on specifying navigation rules in the faces-config.xml, JSFTutorials.net has some great examples.

Happy Coding!



7 responses to Using Navigation Rules in Your XPages Application

Thimo Jansen, May 22, 2011 7:20 PM

Jeremy, are you sure that <from-view-id>NavigationSample.xsp</from-view-id> works? I've tried everything I could come up with but can't get it to work. Its for login.xsp, tried a slash in front of it, with wildcards (like "*login*", but no success. When I replace the "/login.xsp" below with "*", the rule works as expected. Is there something I'm missing?

<navigation-rule>
<from-view-id>/login.xsp</from-view-id>
<navigation-case>
<from-outcome>loginSuccess</from-outcome>
<to-view-id>/home.xsp</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>

Regards,

Thimo


Thimo Jansen, May 22, 2011 6:55 PM

Great post!

A nice addition is that you can add "<redirect/>" to an entry in the faces-config.xml. This is the same as 'redirect="true"' in an XPage navigation rule and sends a redirect back to the browser. Consider the following use-case, without the redirect:

1. User is on /login.xsp, fills correct credentials and clicks"Login"
2. Navigation rule returns the content of /home.xsp, but the URL on the browser still reads "/login.xsp"
3. When the user reloads this page, the login credentials will be posted again to the server

Use of the redirect property prevents this and redirects the browser to the correct URL which is then retrieved with a GET.

Thimo


John Mills, August 9, 2010 12:59 PM

Jeremy,

That was it. It was the type that caused the problem. Works like a charm..
Thanks for all the help..Have a great day..

-John


Jeremy Hodge, August 9, 2010 11:53 AM

@Stephen - Thanks ... Yes, you are right, the nav property panel is there ... I'm just so used to the All Properties that's where I go .. I find, outside the "data" panel, the other property panels to be "fluff" since they don't always provide all the options, and bury the rest of the properties. Personally, I'd like to see the individual property panels go away, and the all properties be given the focus ... the individual property sheets can be helper dialogs from there, but this multiple vectors bit with the individual property sheets is rather pointless and less useful.

As for the use of the faces-config to add navigation rules, its the only way to specify application-wide navigation rules. Granted if you want to manage your rules only at the page level, then you don't have to, but it seems to me to be much more beneficial to standardize your rules across the entire application. This makes maintainability so much easier.

Agreed on the editor for the faces-config too ... if I had some time :)

jeremy


Stephan H. Wissel, August 9, 2010 12:37 AM

Jeremy,
nice one. Love your JSF for DDE series. Now someone needs to write an DDE plug-in to edit the faces-config in a more orderly fashion :-).
Btw. the navigation can be edited in a property panel in a page, no need to go to all properties or the global navigation in faces-config... but it's cool.
:-) stw


Jeremy Hodge, August 8, 2010 9:14 PM

Hey John, can you send me an email with the source code to your class and the content of your faces-config file ... I'll be happy to take a look and see what the deal is.

Also, I'm assuming that in your comment the "HellowWorld" is a typo (the extra w ... if its not, that might be your problem)

Thanks!!

-jeremy


John Mills, August 8, 2010 8:20 PM

Hi Jeremy,

I was trying to follow your article in the xPages blog about Writing a Managed Bean..
http://xpagesblog.com/xpages-blog/2010/7/5/writing-a-managed-bean-to-automate-server-side-functionality.html

I have copied the Java source and xPage source from the article and
added the src folder to the build path and edited faces-config.xml file to include the managed bean.

But, when I open the xPage, it throws an exception

Can't instantiate class: 'com.InfoWorld.MyFirstBeans.HellowWorld'
(My package name is "com.InfoWorld.MyFirstBeans")
It looks like, the xPage is not able to find the class. I am not sure what I did wrong. Is there anything comes to your mind that might have caused the problem?
I know this is the base for all your subsequent articles and I can see a lot of possibilities using this method..Greatly appreciate any light you could shed on this.

Thanks a million..
John