webtier@glassfish.java.net

JSF: variable results with flash, page templates, post-redirect-get

From: <webtier_at_javadesktop.org>
Date: Mon, 09 Aug 2010 12:21:27 PDT

I don't know if this is just my GlassFish installation gone wrong but I'm getting
some very inconsistent results in my application. I use page templates,
implicit page navigation with faces-redirect=true for post-redirect-get, and ajax.

Because of the problem of communicating across pages when using
post-redirect-get I use, or try to use, flash "scope". Because of the problems
I've been experiencing I've ditched request scope in favour of view scoped
managed beans, as a sort of desperate measure, which has seemed improve
things, most of the time, I think.

The sort of thing I'm trying to do is just pass information from one page to
another, an analogy would be to select a department from page 1, click on the
manage employees button, go to page 2 which shows a list of employees. The
problem I'm experiencing is that sometimes page 2 successfully knows which
department it's supposed to be listing employees for, sometimes not.

I've written a test application consisting of 4 files, index.xhtml, next.xhtml,
template.xhtml & BackingBean.java. the index.xhtml has an input field, you
type some text in which is stored directly in the flash, it also has a
preRenderView event, the listener which puts two other items of data in the flash,
one using .put(), one using .putNow(). You click on a button which navigates to
next.xhtml which displays the values from the flash. A button on next.xhtml
takes you back to index.xhtml.

Here are some results I've got from running this test application. server restart
indicates that I've restarted the glassfish server, re-run that I've closed the
browser window and re-run the project from NetBeans.

[code]
server re-run I type displayed on next.xhtml
restart in name1 name2 name3

yes yes aaaa aaaa fred blank
                        bbbb blank blank blank
                        cccc cccc fred blank
                        dddd dddd fred bob
                        eeee eeee fred bob
                        ffff ffff fred bob
             yes aaaa aaaa blank blank
                        bbbb bbbb fred bob
                        cccc cccc fred bob
                        dddd dddd fred bob
             yes aaaa aaaa fred blank
                       bbbb bbbb fred bob
                       cccc cccc fred bob
                       dddd dddd fred bob
yes yes aaaa aaaa blank blank
[/code]

These are the sort of results I'm getting. Even after a server restart you can't
predict what will happen, it's all over the place.

index.xhtml:

[code]
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                template="template.xhtml">
    <ui:define name="metadata">
        <f:metadata>
            <f:event type="preRenderView" listener="#{backingBean.setupPage}"/>
        </f:metadata>
    </ui:define>
    <ui:define name="content">
        <h:form>
            <h:inputText value="#{flash.name1}">
                <f:ajax/>
            </h:inputText>
            <h:commandButton value="Push" action="next?faces-redirect=true">
            </h:commandButton>
        </h:form>
    </ui:define>
</ui:composition>
[/code]

template.xhtml:

[code]
<?xml version='1.0' encoding='UTF-8' ?>
<!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"
      xmlns:f="http://java.sun.com/jsf/core">
    <ui:insert name="metadata"/>
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    </h:head>
    <h:body>
        <div id="content">
            <ui:insert name="content">
                Template Content
            </ui:insert>
        </div>

    </h:body>
</html>
[/code]

next.xhtml:

[code]
<?xml version='1.0' encoding='UTF-8' ?>
<!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:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
    </h:head>
    <h:body>
        <h:form>
            <h:outputText value="name1: #{flash.name1}"/>
            <br/>
            <h:outputText value="name2: #{flash.name2}"/>
            <br/>
            <h:outputText value="name3: #{flash.name3}"/>
            <h:commandButton value="Push" action="index?faces-redirect=true">
                <f:ajax/>
            </h:commandButton>
        </h:form>
    </h:body>
</html>
[/code]

BackingBean.java:

[code]
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;
import javax.faces.event.ComponentSystemEvent;

/**
 *
 * @author Brendan
 */

@Named(value="backingBean")
@ViewScoped
public class BackingBean {

    /** Creates a new instance of BackingBean */
    public BackingBean() {
    }

    public Flash getFlash() {

        return FacesContext.getCurrentInstance().getExternalContext().getFlash();
    }

    public void setupPage(ComponentSystemEvent cse) throws Exception {
        System.out.println("setupPage");
        getFlash().put("name2", "fred");
        getFlash().putNow("name3", "bob");
    }
}
[/code]

I'm using Mojarra 2.0.3 from the Glassfish 3.01 release, running on a Windows 7
server. I'd love to know if anyone else can reproduce these inconsistent results
in their environment, and hopefully that I'm not making some sort of elementary
mistake.

Regards,
Brendan.
[Message sent by forum member 'healeyb']

http://forums.java.net/jive/thread.jspa?messageID=479722