Miles to go …

October 15, 2008

TOTD #48: Converting a JSF 1.2 application to JSF 2.0 – Facelets and Ajax

Filed under: web2.0 — arungupta @ 5:00 am

TOTD #47 showed how to deploy a JSF 1.2 application (using Facelets and Ajax/JSF Extensions) on Mojarra 2.0-enabled GlassFish.  In this blog we’ll use new features added in JSF 2.0 to simplify our application:

  • Use integrated Facelets and resource re-location to simplify our facelets
  • Replace JSF Extensions Ajax API with new in-built JavaScript APIs to expose Ajax functionality.

Let’s get started!

  • Re-create the app as defined in TOTD #47. This app is built using JSF 1.2 core components and Facelets. It uses JSF Extensions for adding Ajax capabilities. Lets change this app to use newer features of JSF 2.0.
  • Edit “faces-config.xml” and change the value of faces-config/@version from “1.2″ to “2.0″.
  • Remove the following fragment from “faces-config.xml”:
        <application>
            <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
        </application>

    This fragment is no longer required because Facelets is the default view technology in JSF 2.0. But it’s important to remember that JSF 2.0 Facelets is disabled by default if “WEB-INF/faces-config.xml” is versioned at 1.2 or older.

  • Remove the following code fragment from “web.xml”:
            <init-param>
              <param-name>javax.faces.LIFECYCLE_ID</param-name>
              <param-value>com.sun.faces.lifecycle.PARTIAL</param-value>
            </init-param>

    This is only required if JSF Extensions APIs are used.

  • Edit “welcome.xhtml” and replace code with:
    <!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:ui=”http://java.sun.com/jsf/facelets”
          xmlns:h=”http://java.sun.com/jsf/html”>
        <ui:composition>
            <h:head>
                <h1><h:outputText value=”What city do you like ?” /></h1>
            </h:head>
           
            <h:body>
                <h:form prependId=”false”>
                    <h:panelGrid columns=”2″>
                        <h:outputText value=”CityName:”/>
                        <h:inputText value=”#{cities.cityName}”
                                     title=”CityName”
                                     id=”cityName”
                                     required=”true”
                                     onkeyup=”javax.faces.Ajax.ajaxRequest(this, event, { execute: ‘cityName’, render: ‘city_choices’});“/>
                        <h:outputText value=”CountryName:”/>
                        <h:inputText value=”#{cities.countryName}” title=”CountryName” id=”countryName” required=”true”/>
                    </h:panelGrid>
                   
                    <h:commandButton action=”#{dbUtil.saveCity}” value=”submit”/>
                    <br/><br/>
                    <h:outputText id=”city_choices” value=”#{dbUtil.cityChoices}”></h:outputText>
                   
                    <br/><br/>
                    <h:message for=”cityName” showSummary=”true” showDetail=”false” style=”color: red”/><br/>
                    <h:message for=”countryName” showSummary=”true” showDetail=”false” style=”color: red”/>
                </h:form>
            </h:body>
            <h:outputScript name=”ajax.js” library=”javax.faces” target=”header”/>
        </ui:composition>
       
    </html>

    The differences are highlighted in bold and explained below:

    • “template.xhtml” is no longer required because standard tags are used to identify “head” and “body”.
    • <h:head> and <h:body> are new tags defined in JSF 2.0. These tags define where the nested resources need to be rendered.
    • <h:outputScript> is a new tag defined in JSF 2.0 and allows an external JavaScript file to be referenced. In this case, it is referencing “ajax.js” script and is rendered in “head”. The script file itself is bundled in “jsf-api.jar” in “META-INF/resources/javax.faces” directory. It adds Ajax functionality to the application.</<br /> li>
    • “javax.faces.Ajax.ajaxRequest” function is defined in the JavaScript file “ajax.js”. This particular function invocation ensures that “city_choices” is rendered when execute portion of the request lifecycle is executed for “cityName” field. The complete documentation is available in “ajax.js”. Read more details about what happens in the background here.

    Notice how the Facelet is so simplified.

  • Refactor “result.xhtml” such that the code looks like as shown below:

    The changes are explained in the previous step, basically a clean Facelet using standard <h:head> and <h:body> tags and everything else remains as is.

And that’s it, just hit “Undeploy and Deploy” in NetBeans IDE and your application should now get deployed on Mojarra 2.0-enabled GlassFish. To reiterate, the main things highlighted in this blog are:

  • Facelets are integrated in Mojarra 2.0.
  • New tags for resource re-location allow a simpler and cleaner facelet embedded in a JSF application.
  • JavaScript APIs provide a clean way to expose Ajax functionality in JSF app.

And all of these features are defined in the JSF 2.0 specification. So if you are using Mojarra then be assured that you are developing a standards compliant user interface.

Have you tried your JSF 1.2 app on Mojarra 2.0 ? Drop a comment on this blog if you have.
File JSF related bugs here using “2.0.0 EDR1″ version and ask your questions on .

Please leave suggestions on other TOTD (Tip Of The Day) that you’d like to see. An archive of all the tips is available here.

Technorati: totd javaserverfaces glassfish mojarra netbeans

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. TOTD #123: f:ajax, Bean Validation for JSF, CDI for JSF and JPA 2.0 Criteria API – all in one Java EE 6 sample application
  2. TOTD #46: Facelets with Java Server Faces 1.2
  3. TOTD #49: Converting a JSF 1.2 application to JSF 2.0 – @ManagedBean
  4. TOTD #120: Deployment Descriptor-free Java EE 6 application using JSF 2.0 + EJB 3.1 + Servlets 3.0
  5. TOTD #135: JSF2 Composite Components using NetBeans IDE – lightweight Java EE 6

8 Comments »

  1. you need correct the title, it say faclets but is facelets

    Comment by vladdy — October 15, 2008 @ 5:42 am

  2. Thanks for catching the typo!

    It’s funny though there 1470 google hits on "faclets" as shown at:

    http://www.google.com/search?q=faclets&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

    Comment by Arun Gupta — October 15, 2008 @ 8:56 am

  3. [Trackback] This is a follow up to TOTD #48 which showed how to convert a JSF 1.2 application to use new features of JSF 2.0. In this blog, we’ll talk about a new annotation added to the JSF 2.0 specification…

    Comment by Arun Gupta's Blog — October 17, 2008 @ 9:52 am

  4. thanx u arun gupta.

    Comment by sinema izle — March 18, 2009 @ 6:59 am

  5. Plz Help me for the below requirement.

    Requirement:
    To Add the Expand collapse functionality in a .jsp to display and hide the data using <rich:dataTable> JSF component.

    Details:

    Here I’m able to get the data from webservices to jsp. While displaying
    the data there is a provision to show and hide some of its data

    Example:

    If Suppose there are 3 rows(Student data) coming from database, Say each row as Student for instance. and Each student is associated with some courses(Say course1, course2 and course3).

    Appearance should look like this:

    Table1:

    Details S.No S.Name S.Address
    ——————————–
    + 10 Alex Texas
    + 20 John Dallas
    + 30 Nick Newyork

    Now if we click on + button associated with first record, it should exapand and show the first record details and – button to be displayed as shown below.

    Table2:

    Details S.No S.Name S.Address
    ——————————–
    - 10 Alex Texas

    course1 : Java
    cpurse2 : j2ee

    - 20 John Dallas

    course1: .Net
    course2: php

    + 30 Nick Newyork

    And when when we click on – button it should hide the data and should display the above data as Table1.

    Code I’ve used in Jsp page:

    Here I got all the data dynamically. The only problem is UI arrangement(Displaying it). I’ve put the S.No, S.Name and S.Address fields in one <rich:columnGroup> and course details in another <rich:columnGroup>. Both columnGroup are embedded in <rich:dataTable>

    Here I am facing a problem while displaying courses for each student(Means expand/collapse or show/hide). Facing problem to include a div or a subTable or something else in dataTable.

    Request you to help me in this regard and let me know in case any details
    required from my end.

    Sincerely,
    Vamshi

    Comment by vamsy krishna — December 14, 2009 @ 10:37 am

  6. Vamshi,

    I don’t know what "rich:" tags stand for. However you should be able to use a component from the several JSF toolkits to fulfill this need. I’ve not done it myself but believe its very much possible.

    Comment by Arun Gupta — February 23, 2010 @ 5:04 pm

  7. Dear Arun Gupta,

    Last week I installed NetBeans 6.9.1, because I need to write a new web application. As JSF 2.0 is the new version I decided to use it. But as all my web apps rely on Tree component, I found that there’s no any Tree component.
    So which is the replacement to Tree component?
    I spent almost 10 days googling for any suitable answer and didn’t find any solution.
    Thanks in advance.

    Francisco Buccafusca
    System Programmer

    Comment by Francisco — November 15, 2010 @ 10:50 am

  8. JSF implementation in GlassFish provides a framework and you are required to use a component library on top of it for widgets like TreeComponent.

    Have you tried using PrimeFaces widget library ?

    Comment by Arun Gupta — November 15, 2010 @ 3:04 pm

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