0

我在 jsp 中有两个按钮,即开始和停止。

预期:只有一次开始按钮应该被点击,计数应该开始,并且当停止按钮被按下时应该停止计数。

(参考代码,当按下停止按钮时,函数stop_action()应该被调用,布尔stopOperation设置为 true 并且 **while 迭代应该在 start_action() 中停止**。)

实际:只要单击“开始”按钮,就会开始新的计数。即使多次按下停止按钮也无法停止计数。

代码如下:

在 faces-config.xml 中, FunctionExit (class, abcwar.FunctionExit) 具有请求范围。

和类 FunctionExit 是:

package abcwar;

import com.sun.rave.web.ui.appbase.AbstractPageBean;
import javax.faces.FacesException;

public class FunctionExit extends AbstractPageBean {

    private boolean stopOperation = false;

    private void _init() throws Exception {
    }

    public FunctionExit() {
    }

    @Override
    public void init() {
        try {
            _init();
        } catch (Exception e) {
            log("FunctionExit Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }

    @Override
    public void preprocess() {
    }

    @Override
    public void prerender() {
    }

    @Override
    public void destroy() {
    }

    public String stop_action() {
        setStopOperation(true);
        System.out.println("getStopOperation() :" + getStopOperation());
        return "true";
    }

    public String start_action() {
        long steps = 0;
        while (!getStopOperation()) {
            steps++;
            System.out.println("Step Count :" + steps + ", getStopOperation() :" + getStopOperation());
        }
        return null;
    }

    public boolean getStopOperation() {
        return stopOperation;
    }


    public void setStopOperation(boolean stopOperation) {
        this.stopOperation = stopOperation;
    }
}
4

1 回答 1

0

(i)在
Session Scoped Class, SessionBean1 中定义函数 start_action()、stop_action() 和类变量 boolean stopOperation
(ii) 使 Stop Button's Disabled Property = !Start Button's Disabled Property

达到预期的行为。

代码如下:

package abcwar;

import com.sun.rave.web.ui.appbase.AbstractSessionBean;
import javax.faces.FacesException;

public class SessionBean1 extends AbstractSessionBean {
    private boolean stopOperation = false;
    private long steps = 0;

    private void _init() throws Exception {
    }

    public SessionBean1() {
    }

    @Override
    public void init() {

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


    }

    @Override
    public void passivate() {
    }


    @Override
    public void activate() {
    }

    @Override
    public void destroy() {
    }

    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    public boolean isStopOperation() {
        return stopOperation;
    }

    public void setStopOperation(boolean stopOperation) {
        this.stopOperation = stopOperation;
    }

    public String stop_action() {
        setStopOperation(true);
        System.out.println("getStopOperation() :" + isStopOperation());
        return null;
    }

    public String start_action() {
        if (steps <= 0) {
            steps = 0;
        }
        setStopOperation(false);
        while (!isStopOperation()) {
            steps++;
            System.out.println("Step Count :" + steps + ", getStopOperation() :" + isStopOperation());
        }
        return null;
    }
}
于 2013-09-10T06:13:19.807 回答