Miles to go …

September 22, 2008

TOTD #46: Facelets with Java Server Faces 1.2

Filed under: javaserverfaces, netbeans, totd — arungupta @ 5:00 am

This blog updates TOTD #45 to use Facelets as view technology.

Powerful templating system, re-use and ease-of-development, designer-friendly are the key benefits of Facelets. Facelets are already an integral part of Java Server Faces 2.0. But this blog shows how to use them with JSF 1.2.

  1. Download Facelets from here (or specifically 1.1.14). Facelets Developer Documentation is a comprehensive source of information.
  2. Add “jsf-facelets.jar” from the expanded directory to Project/Libraries as shown:

  3. Change the JSF view documents to “.xhtml” by adding the a new context parameter in “web.xml” as:
    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
      </context-param>

    The updated “web.xml” looks like:

  4. Specify Facelets as the ViewHandler of JSF application by adding the following fragment to “faces-config.xml”:
      <application>
        <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>   
      </application>

    The updated document looks like:

  5. Create three new XHTML pages by right-clicking on the project, selecting “New”, “XHTML” and name them as “template”, “welcome” and “result”. This creates “template.xhtml”, “welcome.xhtml” and “result.xhtml” in “Web Pages” folder. 
    1. Replace the generated code in “template.xtml” with the code given here. Change the <title> text “Facelets: What’s your favorite City ?”.
    2. Replace the generated code in “welcome.xhtml” with the code given here. Refactor “welcomeJSF.jsp” such that H1 tag and the associated text goes in <ui:define name=”title”> and rest of the content goes in <ui:define name=”body”>. Also change the value of “template” attribute of <ui:composition> by removing “/”. The updated page looks like:

    3. Replace the generated code in “result.xhtml” with the code given here. Refactor “result.jsp” such that H1 tag and the associated text goes in <ui:define name=”title”> and rest of the content goes in <ui:define name=”body”>. Also add a namespace declaration for “http://java.sun.com/jsf/core”.

      Optionally change the <h:form> associated with the command button to:

                      <form jsfc=”h:form”>
                          <input jsfc=”h:commandButton” action=”back” value=”Back”/>
                      </form> 

      The updated page looks like:

  6. Add couple of more navigation rules to “faces-config.xml”:
        <navigation-rule>
            <from-view-id>/welcome.xhtml</from-view-id>
            <navigation-case>
                <from-outcome>submit</from-outcome>
                <to-view-id>/result.xhtml</to-view-id>
            </navigation-case>
        </navigation-rule>
        <navigation-rule>
            <from-view-id>/result.xhtml</from-view-id>
            <navigation-case>
                <from-outcome>back</from-outcome>
                <to-view-id>/welcome.xhtml</to-view-id>
            </navigation-case>
        </navigation-rule>

And that’s it, Facelets-based application is now available at “http://localhost:8080/Cities/faces/welcome.xhtml”. The interaction is exactly similar to as shown in TOTD #45 and some of the sample images (borrowed from TOTD #45) are:

Now this applic
ation is using Facelets as the view technology instead of the in-built view definition framework.
Please leave suggestions on other TOTD (Tip Of The Day) that you’d like to see. A complete archive of all tips is available here.

Technorati: totd javaserverfaces facelets netbeans glassfish

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. TOTD #51: Embedding Google Maps in Java Server Faces using GMaps4JSF
  2. TOTD #48: Converting a JSF 1.2 application to JSF 2.0 – Facelets and Ajax
  3. TOTD #94: A simple Java Server Faces 2.0 + JPA 2.0 application – Getting Started with Java EE 6 using NetBeans 6.8 M1 & GlassFish v3
  4. TOTD #95: EJB 3.1 + Java Server Faces 2.0 + JPA 2.0 web application – Getting Started with Java EE 6 using NetBeans 6.8 M1 & GlassFish v3
  5. TOTD #45: Ajaxifying Java Server Faces using JSF Extensions

2 Comments »

  1. 看不懂,留言一下

    Comment by 一卡多号 — September 23, 2008 @ 11:54 am

  2. [Trackback] Java Server Faces 2.0 specification (JSR 314, EDR2) and implementation (soon to be EDR2) are brewing. This blog shows how to get started with Mojarra – Sun’s implementation of JSF. GlassFish v2 comes bundled with Mojarra 1.2_04 which allows…

    Comment by Arun Gupta's Blog — October 14, 2008 @ 5:55 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
Powered by WordPress