Here we will learn how to navigate from one page to another in JSF 2.0.
In earlier versions of JSF,we used to make entries in faces-config.xml.But now we can simply put the name of the output page in an attribute called "action".
1.FirstPage.xhtml
Here url will not be refreshed since in JSF does Forward in navigation.
If we want that url shud also change, we can add a simple entry like this..
lets take a simple example for illustration.
Controller.java
You can call this method in your xhtml file like this.
FirstPage.xhtml
You can use any approach depending on your requirement.I would suggest to go for first one that is more simple.
Besides this,JSF 2.0 also supports that navigation rule approach that was in earlier version.
You can also go for that depending on your requirement.
There is one another way called "from:action".It comes into picture when two actions are returning
same value or same result.
In this case,we can use "from:action" element to diffrentiate where to navigate.
Lets take a simple examle.
ManagedBean Controller.java
formaction.xhtml
Here when we click on button "Page1" or "Page2",both gives the same result.
So how JSF will determine where to go.
To solve this problem,we can use previous versions technique of jsf.
We can define this in faces-config.xml as a navigation rule.
So JSF can easily understand where to go.
faces-config.xml
In this case,if we click on first button named "Page1",it returns page and directed to Page1 successfully.
and if we click on second button named "Page2",it returns page and directed to Page2 successfully.
JSF page Redirect vs Page Forward
The main difference between these two is "In case of Page forward,whenever Browser sends a request for requested page,server internally redirect it to requested page and returns back the response
to the browser, so url doesnt change.but in case of Page redirect,Whenever a request is made for a particular page,Server redirects response(requested page)
to the browser,then browser again make request for this requested page and then finaally the content is displayed and url is updated in case of
page redirect. "
Conditional navigation
We can easily control navigation using managed beans.
Lets take a simple example.
Navigator.java
The example is self explanatory.Depending on the button clicked,
it will redirect.
In earlier versions of JSF,we used to make entries in faces-config.xml.But now we can simply put the name of the output page in an attribute called "action".
1.FirstPage.xhtml
<h:form> <h:commandButton action="nextPage" value="Move to nextPage.xhtml" /> </h:form>Here when we will click on this button,it will be redirected to nextPage.xhtml.
Here url will not be refreshed since in JSF does Forward in navigation.
If we want that url shud also change, we can add a simple entry like this..
<h:form> <h:commandButton action="nextPage?faces-redirect=true" value="Move to nextPage.xhtml" /> </h:form>There is one another way to navigate by using managed beans.
lets take a simple example for illustration.
Controller.java
@ManagedBean @SessionScoped class Controller implements Serializable { public String moveToNextPage() { return nextPage; } }
You can call this method in your xhtml file like this.
FirstPage.xhtml
<h:form> <h:commandButton action="#{controller.moveToNextPage}" value="Move to nextPage.xhtml" /> </h:form>
You can use any approach depending on your requirement.I would suggest to go for first one that is more simple.
Besides this,JSF 2.0 also supports that navigation rule approach that was in earlier version.
You can also go for that depending on your requirement.
There is one another way called "from:action".It comes into picture when two actions are returning
same value or same result.
In this case,we can use "from:action" element to diffrentiate where to navigate.
Lets take a simple examle.
ManagedBean Controller.java
import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import java.io.Serializable; @ManagedBean @SessionScoped public class Controller implements Serializable { private static final long serialVersionUID = 1L; public String goToPage1(){ return "page"; } public String goToPage2(){ return "page"; } }
formaction.xhtml
<h:body> <h2>welcome</h2> <h:form> <h:commandButton action="#{controller.goToPage1}" value="Page1" /> <h:commandButton action="#{controller.goToPage2}" value="Page2" /> </h:form> </h:body>
Here when we click on button "Page1" or "Page2",both gives the same result.
So how JSF will determine where to go.
To solve this problem,we can use previous versions technique of jsf.
We can define this in faces-config.xml as a navigation rule.
So JSF can easily understand where to go.
faces-config.xml
<faces-config .... <navigation-rule> <from-view-id>fromaction.xhtml</from-view-id> <navigation-case> <from-action>#{controller.goToPage1}</from-action> <from-outcome>page</from-outcome> <to-view-id>page1.xhtml</to-view-id> </navigation-case> <navigation-case> <from-action>#{controller.goToPage2}</from-action> <from-outcome>page</from-outcome> <to-view-id>page2.xhtml</to-view-id> </navigation-case> </navigation-rule> </faces-config>
In this case,if we click on first button named "Page1",it returns page and directed to Page1 successfully.
and if we click on second button named "Page2",it returns page and directed to Page2 successfully.
The main difference between these two is "In case of Page forward,whenever Browser sends a request for requested page,server internally redirect it to requested page and returns back the response
to the browser, so url doesnt change.but in case of Page redirect,Whenever a request is made for a particular page,Server redirects response(requested page)
to the browser,then browser again make request for this requested page and then finaally the content is displayed and url is updated in case of
page redirect. "
Conditional navigation
We can easily control navigation using managed beans.
Lets take a simple example.
Navigator.java
@ManagedBean(name = "navigator") @RequestScoped public class Navigation implements Serializable { //this managed property is used to read value from request parameter. @ManagedProperty(value="#{param.pageId}") private String pageId; //condional navigation based on pageId //if pageId is 1 then go to page1.xhtml, //if pageId is 2 then go to page2.xhtml //else show home.xhtml public String showPage(){ if(pageId == null){ return "home"; } if(pageId.equals("1")){ return "page1"; }else if(pageId.equals("2")){ return "page2"; }else{ return "home"; } } }Now we will code jsf page.
.... <h:form> <h:commandLink action="#{navigator.showPage}" value="Page1"> <f:param name="pageId" value="1" /> </h:commandLink> <h:commandLink action="#{navigator.showPage}" value="Page2"> <f:param name="pageId" value="2" /> </h:commandLink> <h:commandLink action="#{navigator.showPage}" value="Home"> <f:param name="pageId" value="3" /> </h:commandLink> </h:form> ..
The example is self explanatory.Depending on the button clicked,
it will redirect.