开发者

JSF GAE: Value Update Problem in managed bean method

开发者 https://www.devze.com 2023-04-04 06:20 出处:网络
I have the following piece of code with a simple h:outputText pointing to a int and a p:commandLink to set a value:

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(Le开发者_Python百科vel.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.


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:

  • Differences between action and actionListener


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号