Wicket: Where to add components? Constructor? Or onBeforeRender?

Posted by gmallett on Stack Overflow See other posts from Stack Overflow or by gmallett
Published on 2011-02-06T15:21:13Z Indexed on 2011/02/06 15:25 UTC
Read the original article Hit count: 186

I'm a Wicket newb. This may just be my ignorance of the Wicket lifecycle so please enlighten me! My understanding is that Wicket WebPage objects are instantiated once and then serialized. This has led to a point of confusion for me, see below.

Currently I have a template class which I intend to subclass. I followed the example in the Wicket docs demonstrating how to override the template's behavior in the subclass:

protected void onBeforeRender() {
        add(new Label("title", getTitle()));

        super.onBeforeRender();
}

protected String getTitle() {
        return "template";
}

Subclass:

protected String getTitle() {
        return "Home";
}

This works very well. What's not clear to me are the "best practices" for this. It seems like onBeforeRender() is called on every request for the page, no? This seems like there would be substantially more processing done on a page if everything is in onBeforeRender(). I could easily follow the example of the other Wicket examples and add some components in the constructor that I do not want to override, but then I've divided by component logic into two places, something I'm hesitant to do.

If I add a component that I intend to be in all subclasses, should I add it to the constructor or onBeforeRender()?

© Stack Overflow or respective owner

Related posts about java

Related posts about wicket