我在jsp中有两个按钮,即开始和停止。
预期:仅应单击"开始"按钮一次,计数应开始,按下"停止"按钮时应停止计数。
(参考代码,当按下停止按钮时,应调用函数stop_action(),布尔停止操作设置为 true,而迭代应在 start_action() ** 中停止。
实际:每当单击"开始"按钮时,都会开始新的计数。即使多次按下停止按钮也无法停止计数。
代码如下:
在面孔配置.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;
}
}
之后
(i) 定义函数 start_action()、stop_action() 和类变量布尔停止操作在会话范围的类 SessionBean1
(ii) 使停止按钮的禁用属性 = !"开始"按钮的禁用属性
预期的行为已经实现。
代码如下:
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;
}
}