Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I correctly remove an AjaxSelfUpdatingTimerBehavior from a component in Apache Wicket?

Tags:

java

ajax

wicket

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.

like image 463
jjerms Avatar asked Oct 31 '09 14:10

jjerms


1 Answers

I resolved this by using the stop() method instead of attempting to remove the behavior entirely.

I actually do want to remove it entirely at some point after stopping it (because my solution involves newing a behavior up everytime I hit "go" and I want to continue to stop and start without accruing a million behaviors) so I got round that by maintaining a list of behaviors to be removed on some later round trip.

like image 172
jjerms Avatar answered Nov 02 '22 03:11

jjerms