1

Completely edited: Maybe I was mixing problems and misinterpreted. After simplifying my code the question simplifies to: How can I prevent the <p:commandButton> from executing it's action method on page refresh (like when you hit F5 inside browser window)?

JSF Code:

   <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:p="http://primefaces.org/ui">
    <h:body>
        <h:form>
            <h:outputText value="#{bugBean.number}" />
            <h:outputText value="#{bugBean.isComplete()}" />
            <p:commandButton id="entryCommand" value="add"
                action="#{bugBean.increase()}" update="@form" oncomplete="#{bugBean.complete()}"/>
        </h:form>
    </h:body>
    </html> 

backing bean code:

  package huhu.main.managebean;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@Named
@SessionScoped
public class BugBean implements Serializable {

   private static final long serialVersionUID = 1L;
   private int number;
   private boolean isComplete = false;

   public void increase(){
      number++;
   }

   public void complete(){
      isComplete = true;
   }

   public int getNumber() {
      return number;
   }

   public void setNumber(int number) {
      this.number = number;
   }

   public boolean isComplete() {
      return isComplete;
   }

   public void setComplete(boolean isComplete) {
      this.isComplete = isComplete;
   }
}

Update: Even if I remove the oncomplete stuff like this an click the <p:commandButton> just once, the counter goes up on every page refresh.

<h:form>
        <h:outputText value="#{bugBean.number}" />
        <p:commandButton id="entryCommand" value="add"
            action="#{bugBean.increase()}" update="@form"/>
    </h:form>
4

2 回答 2

5

由于缺少头部定义,该构造缺少 Ajax 支持。在这种情况下,我只是在-tag<h:head/>正上方添加,一切正常。<h:body>

感谢所有贡献者!

于 2012-11-02T09:56:21.933 回答
0

我认为increase()每次刷新页面时都不会调用action方法,而是调用了complete()method,这可能会让你认为action方法被调用了。

内部的oncomplete属性p:commandButton表示客户端操作,因此是 JS 方法,而不是服务器操作:EL#{bugBean.complete()}在每次页面刷新时解析它时执行。

于 2012-10-31T21:53:48.747 回答