Web Wicket
Guillermo Castro  

Migrating wicket-example to Wicket 1.2 (beta 1)

UPDATE: I changed the BlogApplication code based on feedback from Igor (one of the Wicket developers). It should now work against the latest trunk from the Subversion repository.

Well, my last entry had some very good feedback from you. It seems people really are interested in learning Wicket, and someone even told me that his boss asked him to evaluate Wicket after reading my article. That kind of reaction for me is both a blessing and a curse. A blessing because I feel good with this kind of response, but a curse (in a good way) because now I want to give more, something that’s not always easy to do, as I do have a day job.

My interest in Wicket comes from the fact that I like the way the framework works. And I’m sure that the more I learn about Wicket, the more I will be able to share with you how to do things. There are a couple of introductory articles already, and the wiki pages also provide good reference points, but there’s nothing like a practical example to understand what’s going on (at least I learn better by example).

And speaking of examples, the example I created for the article used Wicket 1.1.1, but Wicket 1.2 is just around the corner, and barring some last minute critical API changes, it should be stable enough for people to use it.

Migrating the example to 1.2 was actually very easy. All I had to do was to put the new wicket-1.2-beta1.jar file in my lib folder, take out the old jar (of course) and also the ognl jar which is no longer needed, as Wicket now provides its own internal Object-Graph navigation. I also had to modify two classes: BlogApplication and ViewBlogEntry.

The new BlogApplication class now looks like:

package org.javageek.wicket;
import wicket.Application;
import wicket.protocol.http.WebApplication;
import wicket.settings.IMarkupSettings;
import domain.Blog;
import domain.BlogService;

public class BlogApplication extends WebApplication {

    private BlogService blogService = new BlogService();

[i]    @Override protected void init() {
        IMarkupSettings markupSettings = getMarkupSettings();
        configure(DEVELOPMENT);
        markupSettings.setStripWicketTags(true);
        markupSettings.setDefaultMarkupEncoding("UTF-8");
        markupSettings.setStripXmlDeclarationFromOutput(false);
    }

    @Override public Class getHomePage() {
        return Index.class;
    }[/i]

    public static final BlogApplication instance() {
        return (BlogApplication) Application.get();
    }

    public Blog getBlog() {
        return blogService.getBlog();
    }
}

As you can see, in Wicket 1.2 you no longer need to do getPages().setHomePage() in order to set the main page for your web app. Instead, your WebApplication subclass needs to override the getHomePage() method. Also, all the settings should now be set in the init() method. ApplicationSettings no longer exists, and instead has been replaced by specific settings interfaces. A couple of convenience methods were also added, like Application.configure() for direct configuration of an application.

This is how ViewBlogEntry looks like:

package org.javageek.wicket;
import java.text.DateFormat;

import wicket.PageParameters;
[i]import wicket.RestartResponseException;[/i]
import wicket.markup.html.basic.Label;
import domain.Blog;
import domain.BlogEntry;

public class ViewBlogEntry extends BasePage {

    public ViewBlogEntry(PageParameters parameters) {
        super();

        Blog blog = getBlog();
        BlogEntry entry = blog.getBlogEntry(parameters.getString("id"));
        if (null == entry) {
            [i]throw new RestartResponseException(PageNotFound.class);[/i]
        } else {
            add(new Label("entryTitle", entry.getTitle()));
            add(new Label("entryBody", entry.getBody()).setEscapeModelStrings(false));
            add(new Label("pageTitle", entry.getTitle() + " : " + blog.getName()));

            DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, getBlog().getLocale());
            add(new Label("entryDate", df.format(entry.getDate())));
        }
    }
}

The only difference here from the old example is that instead of invoking a method (redirectTo) to go to the 404 page, I now throw a RestartResponseException. This seems cleaner, as I’m basically stoping what I’m doing with the exception. I also don’t need to instantiate a page object in 1.2, but send the class reference as the exception parameter.

The whole migration took me less than an hour, but I wouldn’t use this as a benchmark, as the example is very basic in terms of Wicket functionality. Even though 1.2 breaks a couple of things from 1.1, in my opinion it is a necessary evil in order to put Wicket in a state that makes it even easier to develop things. As before, I’m making the source code available.

Leave a Reply