I'm having trouble with adding and then removing an AjaxSelfUpdatingTimerBehavior in Apache Wicket. The behaviour gets added okay but then as soon as I remove the behavior I get a "Page Expired" come up in the browser very soon, I guess because the removal wasn't clean. My set up is basically a label which starts changing by timer, and two links: "go" and "stop". I want to be able to click "go" and then "stop" (obviously I know it will never work the other way around!). Here's my complete markup:
<html>
<body>
<span wicket:id="message">message will be here</span><br/>
<a wicket:id="go">Go</a><br/>
<a wicket:id="stop">Stop</a>
</body>
</html>
and here's my code:
// imports all from standard wicket
public class HomePage extends WebPage {
private static final int INTERVAL = 500;
public HomePage(final PageParameters parameters) {
final Component label = new Label("message",
"Hello").setOutputMarkupId(true);
add(label);
final IBehavior updater = new AjaxSelfUpdatingTimerBehavior(Duration
.milliseconds(INTERVAL)) {
@Override
protected void onPostProcessTarget(AjaxRequestTarget target) {
label.setDefaultModelObject(String.valueOf(System.nanoTime()));
}
};
AjaxLink<String> go = new AjaxLink<String>("go") {
@Override
public void onClick(AjaxRequestTarget target) {
label.add(updater);
target.addComponent(label);
}
};
AjaxLink<String> stop = new AjaxLink<String>("stop") {
@Override
public void onClick(AjaxRequestTarget target) {
label.remove(updater);
target.addComponent(label);
}
};
add(go, stop);
}
}
I'm using Wicket 1.4.3.
Any help much appreciated. Thanks.