Thor
Thor

Reputation: 6656

JSF GAE: Value Update Problem in managed bean method

I have the following piece of code with a simple h:outputText pointing to a int and a p:commandLink to set a value:

<h:form id="f1">
  <h:outputText id="text" value="#{testBean.index}"/>
  <p:commandLink actionListener="#{testBean.test}" update="text">
    <f:setPropertyActionListener target="#{testBean.index}" value="5" />
    <h:graphicImage url="/images.png"/>
  </p:commandLink>
</h:form>

The managed bean looks like this:

@javax.faces.bean.ManagedBean @ViewScoped
public class TestBean implements Serializable{
  private int index; // getter/setter

  @PostConstruct public void init() {
    index = 0;log.log(Level.WARNING, "@PostConstruct");}

  public void test(ActionEvent ae){
    log.log(Level.WARNING, "Index: "+index);}
}

The bean is constructed correctly, and after the first click on the image the h:ouputText is updated to 5. But in my log message I only see Index: 0 during the first click on the image.

It's something similar like Jsf updates model value with old value, but I have the JSF @ManagedBean annotation.

Upvotes: 2

Views: 713

Answers (2)

BalusC
BalusC

Reputation: 1108722

Action listeners are invoked in the order they're definied in the view. You want to use action instead of actionListener. Even more, the action should in first place have been used to invoke a business action.

<p:commandLink action="#{testBean.test}" update="text">
    <f:setPropertyActionListener target="#{testBean.index}" value="5" />
    <h:graphicImage url="/images.png"/>
</p:commandLink>

See also:

Upvotes: 2

maple_shaft
maple_shaft

Reputation: 10463

What is happening is that the test ActionEvent is getting fired before the request values have been applied.

To get a better understanding of the JSF phase lifecycle and when lifecycle events and ActionEvents fire, implement the Debug PhaseListener as specified in the following blog article.

http://balusc.blogspot.com/2006/09/debug-jsf-lifecycle.html

This should help you understand when request values are being applied, and when events are being fired.

Upvotes: 0

Related Questions