Converting a JSF 1.2 application to JSF 2.0-facelets and Ajax

Source: Internet
Author: User
Tags glassfish
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're all using new features added in JSF 2.0
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
    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:
    <! 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> /> </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/>

    <H: outputtext id = "city_choices"
    Value = "# {dbutil. citychoices}">


    <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 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".
      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
      Javascript file "Ajax. js". This special function invocation ensures
      That "city_choices" is
      Rendered when Execute
      Portion of the Request lifecycle is executed for "cityname" field.
      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
    Shown below:

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

And that's it, just hit "undeploy and deploy" in netbeans IDE and your
Application shoshould 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
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.

From: http://server.dzone.com/blogs/arungupta/2008/10/15/totd-48-converting-a-jsf-12-ap

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.