当前位置: 动力学知识库 > 问答 > 编程问答 >

jsf - Interfere one function from another function in java

问题描述:

I have two buttons in jsp namely Start and Stop.

Expected: Only Once Start button should be clicked, counting should start, and counting should be stopped when Stop Button is pressed.

(With reference to the code, When Stop button is pressed function stop_action() should be called, boolean stopOperation set to true and **while iteration should stop in start_action() **.)

Actual: Whenever the Start button is Clicked, new counting is started. Counting could not be stopped even pressing Stop Button many times.

The code is as follows:

In faces-config.xml,

FunctionExit (class, abcwar.FunctionExit) has request scope.

and class FunctionExit is:

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;

}

}

网友答案:

After
(i) defining the functions start_action(), stop_action() and class variable boolean stopOperation in Session Scoped Class, SessionBean1
(ii) making Stop Button's Disabled Property = !Start Button's Disabled Property

the expected behaviour has been achieved.

The code is as follows:

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;
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: