Main Tutorials

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 :

  1. When button with action=”#{pageController.processPage1}” is clicked, it will return the “success” outcome and move to page1.xhtml
  2. When button with action=”#{pageController.processPage2}” is clicked, it will return the “success” outcome and move to page2.xhtml

Download Source Code

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
27 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
nilesh kale
2 months ago

their is no jsp page named success so from java bean why should we return success directly we can return page1 for 1st method and page2 for 2nd method that easy

Ravi Chand
6 years ago

My jsf project should also support url something like https://www.mkyong.com/{param}, can you please help me how to handle this.

imane
10 years ago

thank you for your explanation
i have one question please:
should we put session scope for managed bean ?

Ariel
10 years ago

#{userMBean.processPage1}
success1
/page1.xhtml

#{userMBean.processPage2}
success2
/page2.xhtml

vrb
11 years ago

https://mkyong.com/jsf2/how-to-delete-row-in-jsf-datatable/
delete button is not working.
It is saying method not found.
Please send an e-mail to me regarding the solution to this problem.
Thank You

11 years ago

It would be nice to know what to return on failure. Is it “fail” or “failure”? Maybe the example could be updated to include that.:)

grim leaper
11 years ago

It does not really matter. It is up to you. You can return arbitrary string from managed bean, e.g. “hello”, “goodbye”. Then you put into navigation-rule something like:

<navigation-case>
  ...
  <from-outcome>hello</from-outcome>
  <to-view-id>greetings.xhtml</to-view-id>
</navigation-case>
<navigation-case>
  ...
  <from-outcome>goodbye</from-outcome>
  <to-view-id>seeyou.xhtml</to-view-id>
</navigation-case>

Have one more look to the tutorial.

Sza?ek
11 years ago

I have a problem. When I click button and I get the message on the same page:

Unable to find matching navigation case with from-view-id ‘/index.xhtml’ for action ‘#{pageController.processPage2}’ with outcome ‘success’

Sza?ek
11 years ago
Reply to  Sza?ek

Sorry, I have problem in my code. Your code is ok.
Great Work..

Veera
11 years ago

Hi MKyong

Tutorial and the explanation is so good. But I am unable to find the replies for the questions posted. I think it will be very useful if discussions are answered.So who reads the tutorial will get perfect knowledge. One of the best tutorial i read…

Really Great Work….

Raghavendran
11 years ago

Can i able to call a java method in navigation attribute?
Because i need to open a screen in the tab controller.

pls assist me..

nahiko
11 years ago

Yes!! Just what I was looking for, it worked like a charm!!

Thanks so much, I just needed to set some values to some variables before navigating through pages.

I changed my code, and the very first time I tried it, it worked 🙂

Cheers!!

xcom
11 years ago

no navigation case match for view id action bean and outcome sucess

Ahmed Adel
11 years ago

Thank you very much

Shirish
11 years ago

Hi Mkyong,

I have following Dir structure

For admin pages

Pages/users/page1.xhtml
Pages/users/page1.xhtml
Pages/admin/page1.xhtml
Pages/admin/page1.xhtml

When I am selecting user page from admin pages, I am not able to access user pages.

Please note that as I am using JSF2, I am providing page name in bean instead of faces-confin.xml

Thanks
Shirish
Suganya
12 years ago

Hai mkyong,
when i clicked page1 it shows success and same t0 page2…how to get the content of the pages

ashish
12 years ago

not working … some problem in this…..

ashish
12 years ago
Reply to  ashish

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’

Alberto
12 years ago

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

tommcatt28
12 years ago

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: [email protected]()
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……

Renato Cesar
13 years ago

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.

santosh
10 years ago
Reply to  mkyong

Please change the link FROM-ACTION and not FORM-ACTION
https://mkyong.com/jsf2/jsf-form-action-navigation-rule-example/

Deepak Rathi
10 years ago
Reply to  mkyong

Hi Yong,
When I am working in JSF2.x then why we are going navigation rule using from-action tag while we can handle this situation by return two different strings from both methods..e.g

public String processPage1(){
return “page1”;
}

public String processPage2(){
return “page2”;
}

JSF_Learner
11 years ago
Reply to  mkyong

I am just wondering why do we need this From-Action Navigation Rule.? Can this scenario be implemented by just using implicit navigation??
In a scenario when we have 2 buttons in the same Form and want to redirect it to 2 saparate page .. then why not have the 2 saparate from-outcome value as “success1” and “success2” and then have there to-viewId as page1.xhtml and “page2.xhtml”

Could any one explain why this from-action would be required??

Rudresh
11 years ago
Reply to  mkyong

Hi Mk
in one page i have 3 Links each links is in separate form, but putting this am not able to navigate from one page another after specifying the navigation rule, can you please tel me why is this not navigating..

navigation Rule as:-

/people/list.xhtml

res
/resource/list.xhtml

customers
/customers/list.xhtml

the same way i have written other links also,

A
10 years ago

This example is bull shit.