JSF GAE: Value Update Problem in managed bean meth

2019-07-15 14:53发布

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.

2条回答
一夜七次
2楼-- · 2019-07-15 15:24

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:

查看更多
来,给爷笑一个
3楼-- · 2019-07-15 15:44

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.

查看更多
登录 后发表回答