JSF “from-action” navigation rule example
In JSF navigation rule, you may encounter a situation where two separate actions return a same “outcome” in a single page. In this case, you can use “from-action” element to differentiate the two navigation cases. See following example :
1. Managed Bean
A managed bean, with two actions which return a same outcome – “success”.
PageController.java
import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import java.io.Serializable; @ManagedBean @SessionScoped public class PageController implements Serializable { private static final long serialVersionUID = 1L; public String processPage1(){ return "success"; } public String processPage2(){ return "success"; } }
2. JSF Page
A JSF page, with 2 buttons linked to the above PageController‘s methods.
start.xhtml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:body> <h2>This is start.xhtml</h2> <h:form> <h:commandButton action="#{pageController.processPage1}" value="Page1" /> <h:commandButton action="#{pageController.processPage2}" value="Page2" /> </h:form> </h:body> </html>
Both actions will return the same “success” outcome, how JSF determine where to go?
3. Navigation Rule
To solve it, defines following navigation rules in the “faces-config.xml“, and use the “from-action” element to differentiate the same “outcome” navigation cases.
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" version="2.0"> <navigation-rule> <from-view-id>start.xhtml</from-view-id> <navigation-case> <from-action>#{pageController.processPage1}</from-action> <from-outcome>success</from-outcome> <to-view-id>page1.xhtml</to-view-id> </navigation-case> <navigation-case> <from-action>#{pageController.processPage2}</from-action> <from-outcome>success</from-outcome> <to-view-id>page2.xhtml</to-view-id> </navigation-case> </navigation-rule> </faces-config>
4. Demo
In above case, the button works like this :
- When button with action=”#{pageController.processPage1}” is clicked, it will return the “success” outcome and move to page1.xhtml
- When button with action=”#{pageController.processPage2}” is clicked, it will return the “success” outcome and move to page2.xhtml








Thank you very much
[...] JSF “form-action” navigation rule example In JSF navigation rule, you may encounter a situation where two separate actions return a same “outcome” in a page. In this case, you can use “form-action” element to differentiate the two navigation cases. [...]
Hi Mkyong,
I have following Dir structure
For admin pages
Hai mkyong,
when i clicked page1 it shows success and same t0 page2…how to get the content of the pages
not working … some problem in this…..
mr mkyong … i’m using from-action not form-action ….. but still it gives an error
Unable to find matching navigation case with from-view-id ‘/index.xhtml’ for action ‘#{myBean.move3}’ with outcome ‘success’
Hi,
I tried the example in Tomcat 7 but I got the error:
GRAVE: Critical error during deployment:
java.lang.LinkageError: loader constraint violation: when resolving interface method “javax.servlet.jsp.JspApplicationContext.getExpressionFactory()Ljavax/el/ExpressionFactory;” the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class, com/sun/faces/config/ConfigureListener, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for resolved class, javax/servlet/jsp/JspApplicationContext, have different Class objects for the type javax/el/ExpressionFactory used in the signature
HI DEAR MKYONG…
I HAVE THIS PROBLEM……I NEED THAT When button with action=”#{“”}” is clicked, it will return the “success” outcome and move to “”.xhtml BUT THE ECLIPSE THROWS ME THE NEXT EXCEPTION:
Caused by: javax.faces.el.MethodNotFoundException: javax.el.MethodNotFoundException: /ICEfacesPage1.xhtml @48,81 action=”#{usuario.getSubmit()}”: Método no hallado: clases.Leausuario@a449e0.getSubmit()
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:92)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
… 23 more
I NEED HELP MY FRIEND……
There is a very little mistake in this tutorial. The tag is called FROM-ACTION and not FORM-ACTION. And I’m just saying that because I liked very much this tutorial and would like to make it better.
Hi Cesar,
haha…my bad, a stupid typo mistake. Article is updated and truly appreciated your help.