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


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:
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.
    • "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 webtier@glassfish.dev.java.net.

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
Comments:

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

Posted by vladdy on October 14, 2008 at 10:42 PM PDT #

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

Posted by Arun Gupta on October 15, 2008 at 01:56 AM PDT #

[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...

Posted by Arun Gupta's Blog on October 17, 2008 at 02:52 AM PDT #

thanx u arun gupta.

Posted by sinema izle on March 17, 2009 at 11:59 PM PDT #

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

Posted by vamsy krishna on December 14, 2009 at 02:37 AM PST #

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.

Posted by Arun Gupta on February 23, 2010 at 09:04 AM PST #

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

Posted by Francisco on November 15, 2010 at 02:50 AM PST #

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 ?

Posted by Arun Gupta on November 15, 2010 at 07:04 AM PST #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today