检查按钮是否被单击一次或两次

时间:2011-10-27 06:12:14

标签: jsf javabeans woodstock

我对JSP中的jsf生命周期和后端bean有疑问:

我有一个按钮,如果它被点击一次或两次我想控制它。我在后面的bean中有一个标志:

protected Boolean flagButtonClicked = false;

我用以下方式控制其行动:

public void selectAllButton_action(){    

    if(flagButtonClicked == false){
        System.out.println("flagButtonClicked == false");
        selectCB.setSelected(true);    
        setDisplayRerun(true);
        flagButtonClicked = true;
    }
    else {    
        System.out.println("flagButtonClicked == true");
        selectCB.setSelected(false);    
        setDisplayRerun(false);
        flagButtonClicked = false;
    }

}

当我点击按钮时,我看到了

flagButtonClicked == false

当我再次点击它时,我再次看到

flagButtonClicked == false

所以它永远不会进入其他状态。是否会发生这种情况,因为我在后面的bean中声明了受保护的Boolean flagButtonClicked = false,无论何时推送它都会再次声明flagButtonClicked?或者还有其他解释吗?

编辑: 这是小脸:

<webuijsf:button actionExpression="#{user$reports.selectAllButton_action}" text="#{msg.report_select_all}"/>

的getter / setter:

public Boolean getFlagButtonClicked() { 
  System.out.println("getFlagButtonClicked is called"); 
  return flagButtonClicked; 
} 

public void setFlagButtonClicked(Boolean flagButtonClicked) { 
  this.flagButtonClicked = flagButtonClicked; 
  System.out.println("setFlagButtonClicked is clicked"); 
}

编辑2: 代码的最后一个版本是:

@ViewScoped
@ManagedBean(name="user$reports", eager=true)
public class reports extends AbstractPageBean {

protected Boolean flagButtonClicked = false;

public Boolean getFlagButtonClicked() {
    System.out.println("getFlagButtonClicked is called");
    return flagButtonClicked;
}

public void setFlagButtonClicked(Boolean flagButtonClicked) {
    this.flagButtonClicked = flagButtonClicked;
    System.out.println("setFlagButtonClicked is clicked" + flagButtonClicked );
}

public void selectAllButton_action(){
if(flagButtonClicked== Boolean.FALSE){
        System.out.println("flagButtonClicked == false");
        selectCB.setSelected(true);System.out.println("selectCB.setSelected(true); ");
        setDisplayRerun(true);System.out.println("setDisplayRerun(true);");
        setFlagButtonClicked(true);System.out.println(" setFlagButtonClicked(true);");
    }
    else {
        System.out.println("flagButtonClicked == true");       
        selectCB.setSelected(false);System.out.println("selectCB.setSelected(false);");
        setDisplayRerun(false);System.out.println("selectCB.setSelected(false);");
        setFlagButtonClicked(false);System.out.println("setFlagButonClicked(false)");
    }
}

输出是:

[#|2011-10-26T21:45:14.645+0300|INFO|glassfishv3.0|null|_ThreadID=29;_ThreadName=Thread-1;|flagButtonClicked == false|#]

[#|2011-10-26T21:45:14.646+0300|INFO|glassfishv3.0|null|_ThreadID=29;_ThreadName=Thread-1;|selectCB.setSelected(true); |#]

[#|2011-10-26T21:45:14.646+0300|INFO|glassfishv3.0|null|_ThreadID=29;_ThreadName=Thread-1;|setDisplayRerun(true);|#]

[#|2011-10-26T21:45:14.646+0300|INFO|glassfishv3.0|null|_ThreadID=29;_ThreadName=Thread-1;|setFlagButtonClicked is clicked true|#]

[#|2011-10-26T21:45:14.646+0300|INFO|glassfishv3.0|null|_ThreadID=29;_ThreadName=Thread-1;| setFlagButtonClicked(true);|#]
[#|2011-10-26T21:46:03.892+0300|INFO|glassfishv3.0|null|_ThreadID=31;_ThreadName=Thread-1;|flagButtonClicked == false|#]

[#|2011-10-26T21:46:03.892+0300|INFO|glassfishv3.0|null|_ThreadID=31;_ThreadName=Thread-1;|selectCB.setSelected(true); |#]

[#|2011-10-26T21:46:03.892+0300|INFO|glassfishv3.0|null|_ThreadID=31;_ThreadName=Thread-1;|setDisplayRerun(true);|#]

[#|2011-10-26T21:46:03.892+0300|INFO|glassfishv3.0|null|_ThreadID=31;_ThreadName=Thread-1;|setFlagButtonClicked is clicked true|#]

[#|2011-10-26T21:46:03.892+0300|INFO|glassfishv3.0|null|_ThreadID=31;_ThreadName=Thread-1;| setFlagButtonClicked(true);|#]

我试图检查bean实例:

public void init() {

        super.init();

        try {
            _init();
        } catch (Exception e) {
            log("recentreports Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }


FacesContext facesContex = FacesContext.getCurrentInstance();
        System.out.println("facesContex.getCurrentInstance();" + facesContex.getCurrentInstance().toString() );
        System.out.println("facesContex.getExternalContext().toString() " + facesContex.getExternalContext().toString());

    }

,输出为:

[#|2011-10-28T11:58:03.130+0300|INFO|glassfishv3.0|null|_ThreadID=32;_ThreadName=Thread-1;|facesContex.getCurrentInstance();com.sun.faces.context.FacesContextImpl@19ea246|#]

[#|2011-10-28T11:58:03.130+0300|INFO|glassfishv3.0|null|_ThreadID=32;_ThreadName=Thread-1;|facesContex.getExternalContext().toString() com.sun.faces.context.ExternalContextImpl@1afa48b|#]

[#|2011-10-28T11:58:03.206+0300|INFO|glassfishv3.0|null|_ThreadID=32;_ThreadName=Thread-1;|flagButtonClicked == false|#]

[#|2011-10-28T11:58:03.206+0300|INFO|glassfishv3.0|null|_ThreadID=32;_ThreadName=Thread-1;|selectCB.setSelected(true); |#]

[#|2011-10-28T11:58:03.206+0300|INFO|glassfishv3.0|null|_ThreadID=32;_ThreadName=Thread-1;|setDisplayRerun(true);|#]

[#|2011-10-28T11:58:03.207+0300|INFO|glassfishv3.0|null|_ThreadID=32;_ThreadName=Thread-1;|setFlagButtonClicked is clicked true|#]

[#|2011-10-28T11:58:03.207+0300|INFO|glassfishv3.0|null|_ThreadID=32;_ThreadName=Thread-1;| setFlagButtonClicked(true);|#]

[#|2011-10-28T11:58:10.513+0300|WARNING|glassfishv3.0|org.apache.catalina.connector.Request|_ThreadID=32;_ThreadName=Thread-1;|PWC4011: Unable to set request character encoding to UTF-8 from context /lrms, because request parameters have already been read, or ServletRequest.getReader() has already been called|#]

[#|2011-10-28T11:58:10.533+0300|INFO|glassfishv3.0|null|_ThreadID=32;_ThreadName=Thread-1;|facesContex.getCurrentInstance();com.sun.faces.context.FacesContextImpl@a1b3e9|#]

[#|2011-10-28T11:58:10.533+0300|INFO|glassfishv3.0|null|_ThreadID=32;_ThreadName=Thread-1;|facesContex.getExternalContext().toString() com.sun.faces.context.ExternalContextImpl@2a92a1|#]

[#|2011-10-28T11:58:10.578+0300|INFO|glassfishv3.0|null|_ThreadID=32;_ThreadName=Thread-1;|flagButtonClicked == false|#]

[#|2011-10-28T11:58:10.579+0300|INFO|glassfishv3.0|null|_ThreadID=32;_ThreadName=Thread-1;|selectCB.setSelected(true); |#]

[#|2011-10-28T11:58:10.579+0300|INFO|glassfishv3.0|null|_ThreadID=32;_ThreadName=Thread-1;|setDisplayRerun(true);|#]

[#|2011-10-28T11:58:10.579+0300|INFO|glassfishv3.0|null|_ThreadID=32;_ThreadName=Thread-1;|setFlagButtonClicked is clicked true|#]

[#|2011-10-28T11:58:10.579+0300|INFO|glassfishv3.0|null|_ThreadID=32;_ThreadName=Thread-1;| setFlagButtonClicked(true);|#]

我担心这篇文章将成为Stackoverflow中最长的帖子:)

1 个答案:

答案 0 :(得分:0)

我不确定,但我认为问题在于你的bean的范围。检查是否在第二次单击时初始化了一个新bean,因为如果它在每次单击时都会执行,则会使用新变量flagButtonClicked初始化一个新bean,并在创建时将其初始化为false。