XML-less JSF Navigations

JSF’s declarative navigation management requires you to specify a navigation rule with navigation cases in faces-config.xml. If your application gets bigger, your faces-config.xml would get bigger as a result. Following the convention over configuration design paradigm, it is possible to avoid navigation rules in xml.

As usual we need to extend JSF and the idea is to plug-in a custom navigation handler. This navigation handler assumes the outcome is the name of the target view of the navigation.

Now without defining an navigation rule in xml, in action binding methods just return the name of the page. For example, to navigate from a paged called login.jsf to mainpage.jsf, return “mainpage”.

public String login() {
//do some stuff
return "mainpage";
}

When the “mainpage” outcome reaches our navigation management, it finds the view id as /target.jsp or /target.xhtml(depending on what view technology you use) and then do a forward. For redirects add the redirect prefix so the string to be returned should be “redirect:mainpage”.

After all there is no need to define any navigation-rule in faces-config so we can get rid of this;