October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

JSF 2.0: Annotations, New Navigation Eliminate XML Configuration

  • March 2, 2010
  • By Sangeetha S, Nitin KL, Ananya S
  • Send Email »
  • More Articles »

Navigation Using Convention Rather than Configuration

Navigation in JSF defines the set of rules for choosing the next view to be displayed after a specified action is completed. In JSF 1.x, navigation was generally specified using the faces-config.xml file.

Consider the following scenario from the online quiz application: When a user completes a question and wants to move to the next question, he/she has to click on the "Next" link that is rendered as shown below:

<h:commandLink value="Next" action="question2"/>


When this link is selected, navigation should proceed from question1.jsp to question2.jsp (assume that views are defined using JSP). This navigation rule should be defined in faces-config.xml as follows:

<navigation-rule>
        <from-view-id>/question1.jsp</from-view-id>
        <navigation-case>
            <from-outcome>question2</from-outcome>
            <to-view-id>/question2.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>


JSF 2.0 introduces two types of navigation to eliminate or reduce entries in the faces-config.xml file:

  • Implicit Navigation
  • Conditional or User Defined Navigation

Implicit Navigation

Implicit Navigation requires no entry in the configuration file. It allows the default navigation handler to choose the XHTML page with the matching name as specified in the action attribute of the UIComponent.

Suppose navigation from question1.xhtml to question2.xhtml (using Facelets as the view technology) should occur on the click of a link.

<h:commandLink value="Next" action="question2"/>


With implicit navigation, the value of the action attribute is treated as the to-view-id. The default navigation handler appends a '/' and .xhtml extension, and navigation proceeds from question1.xhtml to question2.xhtml. In case a bean method is invoked as part of the action, the String values returned by the method are also treated the same way.

Implicit navigation simplifies the navigation model greatly when compared with pre-JSF 2.0, as there is no need to define the navigation rules explicitly.

Conditional Navigation

Conditional navigation enables you to set a pre-condition for navigation. This pre-condition needs to be met before allowing navigation to the specific view specified in the navigation case in faces-config.xml. The pre-condition is specified using an EL expression and the &tl;if> element:

<navigation-case>
   <from-action>#{admin.buyStock}</from-action>
   <if>#{user.admin}</if>
   <to-view-id>/order.xhtml</to-view-id>
</navigation-case>


The <if> tag in the navigation case ensures that the navigation proceeds to order.xhtml only if the present user is an administrator.

Conclusion

In this article, we discussed the two exciting new features of the JSF 2.0 specification: the introduction of annotations and the new navigation convention. Conventional navigation replaces configuration, and annotations in managed beans makes development really easy. JSF 2.0 will definitely be a more developer-friendly framework.

Stay tuned as we will explore more notable new features in JSF 2.0 in upcoming articles.

Acknowledgements

The authors would like to sincerely thank Mr. Subrahmanya (SV, VP, ECOM Research Group, E&R) for his ideas, guidance, support and constant encouragement and Ms. Yuvarani Meiyappan (Lead, E&R) for kindly reviewing this article and for her valuable comments.

About the Authors

Sangeetha S. works as a Senior Technical Architect at the E-Commerce Research Labs at Infosys Technologies. She has over 10 years of experience in design and development of Java and Java EE applications. She has co-authored a book on 'J2EE Architecture' and also has written articles for online Java publications.

Nitin KL works at the E-Commerce Research Labs at Infosys Technologies. He is involved in design and development of Java EE applications using Hibernate, iBATIS, and JPA.

Ananya S. works at the E-Commerce Research Labs at Infosys Technologies. She is involved in design and development of Java EE applications using Hibernate, iBATIS, and JPA.


Tags: annotations, JavaServer Faces, JSF 2.0, JSF, navigation



Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel