The Source for Java Technology Collaboration
User: Password:



Ed Burns

Ed Burns's Blog

Repost: Bringing Ruby on Rails's Flash to JSF

Posted by edburns on March 01, 2006 at 12:20 PM | Comments (5)

With the demise of the Sun Engineer's Sandbox, my content posted there has disappeared. Thankfully I saved a copy. Thanks to srcerer on the ##jsf chat room on freenode for reminding me to repost this.

Overview

To celebrate the release of Ruby on Rails (RoR) 1.0, I have taken the flash concept from Rails's ActionController and brought it to JSF. For those unfamiliar with RoR or the flash, the flash is basically a map whose entries have a finite, container enforced lifetime. The concept is familiar; it's the same old "scope" concept used in the "request", "session" and "application" classes in the Servlet spec. The point of the flash is to hold information you discover on this request, that you want to use on the next request, but want to be forgotten after the next request is finished

Figure 1 is a cheesy graphical depiction of the request, flash, dialog, session, and application scopes.

The nice thing about these scopes is that they all have a Map into which you can stick things. These Maps have the special property that they will be cleared when the scope ends. In the case of the flash, the map is cleared after exactly two runs through the JSF request processing lifecycle.

I included Shale's Dialog concept for good measure since I believe this concept is important to the future of JSF For example, it's so important that JBoss Seam also provides a similar concept. A Shale dialog or Seam conversation is a scope whose length is determined by the application developer.

Background

Of course, I needed a release-vehicle for this thing so I created a new glassfish sub-project on java.net called jsf-extensions. My intent is to use this project for a number of extensions to the core jsf implementation. The flash is just the first the first that is ready for public use. Others in the works include:

  • A completion and implementation of Jacob Hookom's JSF Avatar proposal

  • A generalization of the Sun JSF implementation's JSP tag library generator. This tool is used in the Sun implementation to generate the TLD and tag handlers for the html_basic taglib from the existing standard JSF component metadata in the standard-html-renderkit.xml file. The idea is to generalize this tool so it can be used to generate the taglib for any JSF component.

  • A generalization of the PhaseListener approach used in the Sun Blueprints Solutions Catalog AJAX components to serve up JavaScript files so that it can serve up any file out of a component jar. This problem is solved in MyFaces using the Extensions Filter but a PhaseListener is a more self-container approach. This problem is also solved by the Weblets Project but personally I don't think the problem of loading resources is big enough to warrant an entire top-level project. Also, weblets has extra XML configuration steps that simply are not necessary if you use the PhaseListener approach.

But enough about the future of jsf-extensions, let's quickly examine how to use the flash by examining the sample war included in the jsf-extensions download.

Getting Started

  • Download and unpack the jsf-extensions download.

  • Download a recent build of glassfish, I used Build 31.

  • Start it up by going to the bin directory and running:

    ./asadmin start-domain domain1
  • Then deploy the sample app by executing:

    ./asadmin deploy --user admin --password adminadmin ~/jsf-extensions-1.0alpha1/wars/run-time-test-0.1.war

    Of course, you need to adjust your path to the war accordingly. The app will then be accessible at http://localhost:8080/run-time-test-0.1/. This will take you to an index page where you should click on the "Ruby on Rails-style Flash" link.

App Traversal

This app illustrates using the Flash from JSP. It is also possible to use the flash directly from Java Code, or via the EL API. These latter two methods are described in the JavaDocs.

As you may know from Jennifer and Pierre's article on the Unified EL, JSF expressions can be accessed in a "get" context, or a "set context", otherwise known as "rvalue" or "lvalue", respectively. Thefore, the EL Expression #{flash.foo} will store a value into the hash under the key "foo" when evaluated as an lvalue, while it will retrieve the value under key "foo" from the flash when evaluated as an rvalue.

In JSP pages, most expressions act as rvalues, whereas the lvalue behavior does not occurr until the form is submitted and the values entered by the user are stored into the expressions. However, to make it easier to set expression values from JSP, including setting into the flash, the jsf-extensions library includes the jsfExt:set tag. A simple usage of this tag found on the first page in the sample app, flash.jsp, is shown below.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>RoR Flash Test Page 1</title>
    <%@ taglib uri="http://java.sun.com/jsf/core"  prefix="f" %>
    <%@ taglib uri="http://java.sun.com/jsf/html"  prefix="h" %>
    <%@ taglib prefix="jsfExt" uri="http://java.sun.com/jsf/extensions" %>
  </head>

  <body>
<f:view>

  <h:form id="form1">

  <h:panelGrid columns="2" border="1" width="600">

    Store into the flash when rendering this page      

    <jsfExt:set var="#{flash.foo}" value="fooValue" />

    Value of <code>\#{flash.foo}</code>, should be <code>null</code>.

    <h:outputText value="#{flash.foo}" />

    <h:commandButton value="reload" />

    <h:commandButton value="next" action="next" />

   </h:panelGrid>

  </h:form>

</f:view>
  </body>
</html>

We're using the panelGrid tag (in way we not possible before JSF 1.2, due to the problems pointed out in Hans Bergsten challenge) to lay out the contents in a simple two column table. The first row shows the use of the jsfExt:set tag to store a value into the flash. The second row shows how to get things from the flash. When viewing this page the first time, you won't see anything in the "Value of #{flash.foo} row" until you press the "reload" button in the page. This is because the normal use-case of the flash is to store things in this request that will be accessed on the next request.

If you want to store something in the flash for use on this request, click on the "next" button in the sample app for an example. In this page we use the "flash.now" syntax, shown below in JSP.


<jsfExt:set var="#{flash.now.bar}" value="barValue" />

<h:outputText value="#{flash.now.bar}" />

By inserting the special keyword "now" we tell the flash that this store operation should be accessible on this request, rather than the next request. If, during processing, you decide you want to promote the value from flash.now to the real flash, use "flash.next" as shown on the next page of the sample app.


<f:verbatim>
<jsfExt:set var="\#{flash.now.buckaroo}" value="banzai" />
</f:verbatim>

Value of <code>\#{flash.now.buckaroo}</code>, should be
<code>banzai</code>.

<h:outputText value="#{flash.now.buckaroo}" />

Promote buckaroo to stick around for the next request.

<jsfExt:set var="#{flash.keep.buckaroo}" value="#{flash.now.buckaroo}" />

When you click on the next button, you will be shown a page that shows that the value in the flash has indeed survived the postback.


Show that buckaroo is still here.

<h:outputText value="#{flash.buckaroo}" />

This is a brief introduction to using the flash, a more in-depth article would cover a common real-world use-case, such as doing master-detail, and also cover the other two entry points to using the flash, the EL API, and direct programmatic use.

Technorati Tags:


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Great repost, Ed. Folks have recently asked me about new features for JSF and wondered whether things like this were doable in JSF. Now I know where to direct them.

    Posted by: vsingleton on March 01, 2006 at 05:14 PM

  • Hi Ed, thanks for mentioning the Weblets project on Java.net.


    I'm curious to know more about your idea of completely eliminating the need for XML syntax while using the PhaseListener approach for JSF resources. Do you mean that each component library or Javascript library would provide a separately coded PhaseListener instance?


    Weblets currently provides a single general purpose PhaseListener that is implicitly registered for such JSF resource loading across any number of different component libraries. The META-INF/weblets-config.xml in each JAR is used to publish the resources packaged in the same JAR that need to be exposed via URL.


    If you have some ideas for improvements to the current approach, please let us know on the Weblets development mailing list.

    Posted by: jfallows on March 19, 2006 at 05:09 PM

  • Ed,

    What is your take regarding this flash scope compared to the use of Tomahawk's saveState component? I've used saveState extensively in the past and found it worked well for transitioning managed bean values from request to request. Are the two meant to tackle the same problem space?

    Perhaps one downside side I see right off to the flash scope is that it is backed by the session which could impact back button compatibility. What do you think?

    Mike

    Posted by: youngm on April 10, 2006 at 02:40 PM

  • Hello Mike,

    We in the EG agree that the conversation/flash/dialog mechanism that are proliferating now need to be standardized. Yes, the flash and savestate are meant to tackle the same problem space. I suppose it could also be implemented with a separate cookie if you like.

    Ed (JSF co spec lead)

    Posted by: edburns on April 11, 2006 at 08:47 AM

  • We're using the panelGrid tag (in way we not possible before JSF 1.2, due to the problems pointed out in Hans Bergsten challenge) to lay out the contents in a simple two column table. The first row shows the use of the jsfExt:set tag to store a value into the flash. The second row shows how to get things from the flash. When viewing this page the first time, you won't see anything in the "Value of #{flash.foo} row" until you press the "reload" button in the page. This is because the normal use-case of the flash is to store things in this request that will be accessed on the next request.

    If you want to store something in the flash for use on this request, click on the "next" button in the sample app for an example. In this page we use the "flash.now" syntax, shown below in JSP.


    acrobat
    adaware
    antivirus
    antivirus
    ares
    avatar

    avatars
    clone
    divx
    download
    edonkey
    emoticone

    emoticones
    emule
    firefox
    gratuit
    gratuite
    icq

    kazaaa
    limewire
    logiciel
    logiciels
    messenger
    morpheus

    msn
    nero
    realplayer
    shareaza
    skype
    spybot

    telechargementnu
    telechargerdas
    pitelechargerant
    tytelechargerant
    frtelechargerant


    vlcolo
    winampaww
    rrewinmx

    tiwinrar
    powinzip

    dedamateur
    amateurdes
    eeranalu
    aseanime
    okoasian
    duiass
    babere6

    fubbw5
    bdsm55
    derbeach4
    bisexualpo
    bizarrecu
    axsblack


    deblonde5
    tyhlowjob
    cobondage
    gubooty
    vhuvukkake
    ddecartoon

    buexotic
    celebriteuy
    plitoris44
    crwcloseup
    edecoock
    deacouple

    qecreampie
    sripumshot
    judeepthroat
    ggildo66
    tredoublepenetra
    verdrunk
    lopebony

    treethnic
    bufaceh
    mifacial
    mhhofat
    frefemdom
    fetishhh55

    dreffmm
    tyfingering
    fist timefre
    istingde44f
    flashingsa
    footjobre

    uckingdas
    banggangtre
    gay23i
    gayssw
    gloryghole
    gugranny
    hairypo

    andjobde
    hardcorereh
    hentaityy
    housewifesaw
    indianerrh
    interracial223

    guujapanese
    latexree
    latinatyy
    lesbianvgh
    lingerieww

    guumaid
    ffmardigrasre
    pasturbation
    yumature
    remidgeth
    demilfg

    frmmf
    modeluy
    nudeerh
    nudistcre
    oralvre
    orgyoko

    greoutdoor
    pantiereh
    pissingdas
    plumperdes
    ornstaroko
    pregnantsas

    repuffy
    ushsy
    redheadca8
    rimjobse
    schoolgirlas44
    shavingde

    sshemale66
    showerbathh
    smokinghh
    softcoreh
    spankinghh
    squirting99

    eshstories
    tystrapon
    reswinger77
    mateenghy
    nnthong34
    uythreesome

    destits
    toesssa
    toonty44
    toynum
    trannyoi
    uglyplo44

    ervirgin
    tyuvoyeur
    drewebcam
    loweird9
    yuwetshirt

    pofilmgratuit
    piorngratuit
    poxxxgratuit
    paornogratuit
    losexgratuit
    tysexegratuit


    ggyxgratuit
    oitelechargement
    plotelechargemen
    jutelechargement
    atelechargemorno

    eetelechargement
    etelechargemsexe
    utelechargementx
    tytelechargermov
    lltelechargerfil

    aytelechargerorn
    ketelechargerorn
    vtelechargersex5
    otelechargersexe
    vytelechargerx
    telechargerxxx76


    gtdosex55
    pororno
    lamovie77


    sdfaged
    ffamazing22
    adanime
    ass3hole334
    hybabe00
    jubackseat

    juibang
    lobathing
    frbeachhh
    vebeauty
    bybeaver5
    hubed99

    bubiggtit
    lobimboh
    nubizarre
    beboyh
    bybusty
    yybutt

    nucheerleader
    michickk
    kichinese
    lochubby
    beclassyy
    nyplit

    frcoed
    buicorset
    kicum77
    likunt
    nucurly
    cadick77

    dedirtyy
    sedolll
    aedorm
    redressss
    ggdrilled
    trdrinking

    uyfoot
    ffgirlfriendd
    iglasses55
    gorgeous77ii
    goth22uu
    grannyf0

    hiddenvv9
    hirsute56
    hooterby66
    hornygr55
    hugegats
    jerkingjju

    jizas
    juicyce66
    kinkyffr
    kissing55
    leg66ii
    lickggh


    malehy
    nasturbatingr
    melonfu
    nistressre
    momase44y

    nakedaas
    naturalder
    naturetre
    naughtyvu
    nippleswe
    nymphoww

    oiled22
    older99uu
    orgasm32
    orientalvb66
    outsiderr66
    pantytr

    penetratefrr
    petitekki
    pinklo88
    plumppe
    ornstarve
    prettyty77

    privateru
    skinnyde
    szlutda44y
    smokesa55i
    spankserr
    zzs3permfrss

    spreade3
    stockingrr5
    stripe4ii
    strippertr
    studses
    studentad

    suckingase
    sybiancv
    tall334y
    teaseuy
    toplesszass
    trannycre

    tubash5h
    twinkse
    undressingre
    baginao
    bibratorry
    vixentre

    wwhooreedd
    wivesas

    Posted by: sallee on October 11, 2006 at 02:11 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds