我有以下一段代码,其中有一个简单的 h:outputText
指向一个 int
和一个 p:commandLink
来设置一个值:
<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>
托管 bean 如下所示:
@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);}
}
bean 构造正确,在第一次单击图像后,h:ouputText
更新为 5。但是在我的日志消息中,我只在第一次单击图像时看到 Index: 0
。
类似于Jsf updates model value with old value ,但我有 JSF @ManagedBean
注释。
最佳答案
Action 监听器按照它们在 View 中定义的顺序被调用。您想要使用 action
而不是 actionListener
。更重要的是,action
应该首先用于调用业务操作。
<p:commandLink action="#{testBean.test}" update="text">
<f:setPropertyActionListener target="#{testBean.index}" value="5" />
<h:graphicImage url="/images.png"/>
</p:commandLink>
另见:
关于java - JSF GAE : Value Update Problem in managed bean method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7361734/