myfaces-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Simon Lessard" <simon.lessar...@gmail.com>
Subject Opinion on a possible page flow scope bug
Date Fri, 28 Sep 2007 19:35:21 GMT
Hi all,

We have a client who played with page flow scope and bit and came to the
following. Of course, the use case is extremely synthetic, but it's still
strange:

page.jspx
<f:view>
  <tr:document>
    <tr:form>
      <tr:inputText value="#{bean.value}"/>
      <tr:commandButton text="Go"/>
    </tr:form>
  </tr:document>
</f:view>

Bean.java
public class Bean
{
  public Bean()
  {
    RequestContext rContext = RequestContext.getCurrentInstance();

    Map<String, Object> pageFlowScope = rContext.getPageFlowScope();

    if (!pageFlowScope.containsKey("someKey"))
    {
      System.out.println("Creating the long to create object and we sure
don't want to create it twice for the page flow");
      pageFlowScope.put("someKey", getAnExpensiveToCreateObject());
    }
  }

  public String getValue()
  {
    RequestContext rContext = RequestContext.getCurrentInstance();

    Map<String, Object> pageFlowScope = rContext.getPageFlowScope();

    return ((SomeClass)pageFlowScope.get("someKey")).getStringAttribute();
  }

  public void setValue(String value)
  {
    RequestContext rContext = RequestContext.getCurrentInstance();

    Map<String, Object> pageFlowScope = rContext.getPageFlowScope();

    ((SomeClass)pageFlowScope.get("someKey")).setStringAttribute(value);
  }
}

With that code, the expensive object is going to be created twice, when the
page is first rendered and on first postback. This happen because at the
time the form is rendered, the page flow scope is still empty (bean was
never referenced) and the default behavior in that case is to not add the
token to the action url thus losing the object. Therefore, if you change the
page to

page.jspx
<f:view>
  <tr:document>
    <tr:outputText value="#{bean.value}"/>
    <tr:form>
      <tr:inputText value="#{bean.value}"/>
      <tr:commandButton text="Go"/>
    </tr:form>
  </tr:document>
</f:view>

Then the expensive object get created only once. Personally I think we
should consider that a bug as it's very counter intuitive and hard to debug.
Am I missing something?


Regards,

~ Simon

Mime
View raw message