- 目的:continuation的出现,替代了通过session或者隐藏参数保存数据的方式,它是使用java语言本身的特性来控制状态
- 目前发展状况:当前我使用的是webwork2.2.7版本,在其版本中明确说明,目前该功能还不成熟,目前建议在大型系统中不要使用该功能。
- 其他限制条件:在ExecuteAndWait 和Token拦截器中,不能使用continuaction,因为ExecuteAndWait和Token都会限制同一个Session的请求次数, 也就是说他们不"期望"看到相同的请求
- 用法介绍
a)类必须继承ActionSupport并且实现Preparable接口
b)在execute方法中,必须调用父类的pause方法。pause方法只能在execute方法中调用。
c)在prepare方法中,必须要调用父类的clearErrorsAndMessages();方法。其目的是,清楚上次请求后产生的field errors 和action errors,如果不清楚,会产生意想不到的错误。以下是webwork中关于这点的原始解释:// We clear the error message state before the action.That is because with continuations, the original (or cloned) action is being executed, which will still have the old errors and potentially cause problems,such as with the workflow interceptor
d)在webwork.propeties配置文件中,一定要定义webwork.continuations.package=包,其目的是告诉webwork,那个包将会使用到continuation属性
-
原理阐述
使用continuations特性必须由WebWork管理流程状态, 因此, 这要求应用程序必须将流程的ID告知WebWork.。WebWork使用一个名为 continue 的参数为流程中的每一次请求提供一个唯一id来做到这一点.。如果使用URL或Form标签来产生URL链接, continue 参数将会自动生产。 如果没有使用这些标签, continuations不会正常工作。当你提交action的时候,使用get方法,那么将会在地址栏中看到这个参数:__continue=b1683b46f8cade05eccbbb39d2c2b8ac,这个参数是通过webwork自己生成和维护的。
- 示例代码,配置文件省略
public class PauseTestAction extends ActionSupport implements Preparable {
private static final Log LOG = LogFactory.getLog(PauseTestAction.class);
private int guess;
public void prepare() throws Exception {
// We clear the error message state before the action.
// That is because with continuations, the original (or cloned) action is being
// executed, which will still have the old errors and potentially cause problems,
// such as with the workflow interceptor
clearErrorsAndMessages();
}
public int getGuess() {
return guess;
}
public void setGuess(int guess) {
this.guess = guess;
}
public String execute() throws Exception {
int answer = new Random().nextInt(100);
int tries = 5;
while (answer != guess && tries > 0) {
pause(Action.SUCCESS);
if (guess > answer) {
addFieldError("guess", "Too high!");
} else if (guess < answer) {
addFieldError("guess", "Too low!");
}
tries--;
}
if (answer == guess) {
addActionMessage("You got it!");
} else {
addActionMessage("You ran out of tries, the answer was " + answer);
}
return Action.SUCCESS;
}