This is Second  part of JSF 2.0 discussion. Please visit part one 

Update : New Post JasperReports in JSF

In JSF 2.0 part 1  post, i used  @ManagedBean annotation for POJO java class. Its OK, actually it derived from JSF 1.2.But we should use @Named annotation instead of @ManagedBean, if CDI (Contexts and Dependency Injection) environment is available. I used Tomcat 7, its not Java EE 6 server and by default CDI is not available.

 But we can use JBoss's Weld lib for achieve CDI in tomcat with minimal configuration. Weld is reference implementation of CDI and its comes standalone version also. This standalone version weld provide CDI environment for both Web Application and Java SE application. By default Glassfish 3.0/3.1 server comes with weld  library for CDI Environment. Here i am using Glassfish server with CDI enabled.

Page Navigation
       This post we will discuss page navigation in JSF2.0. Page Navigation means flow of the application. From one page to another page. In JSF1.2 page navigation is achieved through faces-config.xml configuration file. But JSF 2.0 that configuration file is optional. JSF2.0 gives new feature is Implicit Navigation. So in JSF2.0 we can use page navigation in two ways either implicit navigation or through configuration files. we will see the both.

Implicit Page Navigation
   In Implicit navigation we can directly provide the next flow page in action attribute of JSF 2.0 tags. For example, we show one submit button in index.xhtml, when we click that button we should go to welcome.xhtml page

here action attribute just specify the welcome or welcome.xhtml directly. JSF 2.0 just pick the specific xhtml page and render that page.

Through faces-config.xml file Page Navigation
   In JSF 1.2 navigation flow is achieved only through faces-config.xml. Its look like navigation file in Struts.


In above JSF 2.0 code snippet they have two Submit Button. One have simple "success" literal in action attribute and another have one method in action attribute that method just return same "success" literal. Now JSF 2.0 first check in faces-config.xml, any navigation cases are match to the string literal, if matched then which to view id mentioned in faces-config.xml that page is rendered. even though success.xhtml page is found. If not found in navigation cases then JSF2.0 try to do implict navigation. If implicit navigation fails then it show to client, page navigation is failed due to that page is not found.
So one thing is very important. faces-config.xml always take the high priority than implicit navigation, If its present.
Forward Vs Redirect

    Here we will discuss difference between forward and redirect. In JSF 2.0 by default it use forward only. For example if u run above example you can get following screen shots
when we click any submit button we get following welcome.xhtml page content. but still address bar pointing out index.xtml

We will discuss how forwarding works in JSF2.0. if user clicks submit button post the values, if any, to index.xhtml. In index.xhtml page return the success string literal. In both implicit and explicit page navigation find appropriate welcome.xhtml and render that page and return the content to browser. Browser is not aware that result is return by same page or different page. So browser still think about result content is return by same page.
Forwarding in JSF 2.0

    If you want to achive redirect in JSF 2.0. we should modify little bit. In Implicit Navigation we need to append "?faces-redirect=true". then JSF 2.0 consider this navigation ask redirect instead of forwarding.
If you want redirect through faces-config.xml file page navigation, then add <redirect/> tag in side <navigation-case> tag.


If we run the code with <redirect/> tag or ?faces-redirect=true
when user click the any submit button its shows welcome.xhtml page. Now see the address bar of the browser. Now its shows welcome.xhtml

How Redirect is works?.
    Redirect is other hand. Now user click the submit button. Browser send the post the data to index.xtml. now index.xtml is processed. it return success literal and checks whether any redirect tag or implicit navigation ?faces-redirect=true are present or not. If it present the it simply return to browser with HTTP Status code 301. Its means the resource are temporally moved to another location welcome.xhtml. When the browser gets 301 status code the again connect to new resource welcome.xhtml. Now welcome.xhtml processed in server and return the content. Now browser render and display the content. So when browser get the resource its update that URL into address bar. so now we get welcome.xhtml in address bar.
Redirect in JSF 2.0

Here Forwarding have some advantage over redirect.

  • Forwarding makes both browser and server have more overhead and 2 connection need to establish.
  • Redirect solve the form re submission problem. and its good design 

    Forward is establish  two HTTP  connection. so overhead for both client and server. In some situation forward is best approach. consider the scenario you have one registation.xhtml page for registration form. User fill the data and click submit form fields are posted to server and updated either DB or List and return success/failed message  page show to user. In the case success/failed message page is shown but address bar is still point to  registation.xhtml.  Because its forwarding, so same URL is showing. Now user  click refresh button. then what happen? that page  is re-submitted every time when we click refresh. That issue we can solve through redirect.

See the new screencast about this problem:

 Please visit part three

Comments are welcomed