Get Ready for Wicket 1.4’s New Java 5 and Spring Features

have been reading the buzz about the Apache Wicket web application framework for a while, but didn’t decide to seriously learn Wicket until just a few months ago. I had just finished experimenting with the Java Persistence API (JPA), and decided to repurpose some of the code into a web application for managing bugs. Yes, there are many bug-tracking applications out there, Bugzilla, Jira, and all their cousins, but none compared to the thrill of rolling my own.

Just as I finished writing up the basic bug-tracking functionality to my satisfaction, along comes a new version of Wicket, 1.4-M1, a milestone on the way to 1.4. Of course I had to try it. This article walks you through my experience. Follow along to learn how to utilize some useful new features slated for Wicket 1.4.

Move to Java 5
Wicket 1.4 is Java 5 only. So open up your Maven project’s pom.xml file, and make sure that the source code is Java 5 compliant. You can add the following compiler customization to the plugins part of the build section in the pom.xml:

  maven-compiler-plugin      5    5  

Once you recompile successfully with this compiler customization, you have taken the first baby-step to using Wicket 1.4.

Next, fire up Eclipse and make the corresponding changes to your Wicket project to get Eclipse to recognize Java 5 code. This is necessary because you will be using generics and other fun Java 5 stuff, as well as getting rid of ugly casts.

Now, get ready to step out onto the bleeding edge. Instead of using Wicket 1.4-M1, use the nightly snapshots. To do this, use the Apache snapshot repositories by adding the following lines to your pom.xml:

      Apache    apache    http://people.apache.org/repo/m2-ibiblio-rsync-repository    default            true            wicketstuff    Wicket Stuff    http://wicketstuff.org/maven/repository    default        true      

Next, change the version of Wicket in your pom.xml to 1.4-SNAPSHOT and recompile. Knock on wood and hope the build works.

Fire up Eclipse again and get ready to use generics. I have the sonatype plugin installed, because it allows me to use the pom.xml to manage my dependencies from within Eclipse. The sonatype plugin also allows me to download the source code for all the open source projects that I use. Have Wicket’s source code handy, in case you need to get into it while debugging.

Make Everything Generic
The most interesting change to Wicket in 1.4 that models use generics. So you do not have to cast to get back your model object. All components are “templated,” including panels, links, images, forms, form components such as labels, text boxes, and drop-down choices, and so on. However, it is a good idea to start cautiously. I like to have my code in source code control, in case I do something stupid and need to revert.

Start with the web application and session classes and see if these need any changes. I used AuthenticatedWebSession and noticed that it had a deprecated constructor that I had to change. Next, carefully start updating your models to use templates. The template type should be your model object’s class. In my bug-tracking application, this was usually my Bug class. The trick is to make a small change, test it thoroughly, and then check it back into source control.

After updating all your models, move on to forms and form elements. All your textboxes and drop-down lists can be specialized using generics. Forms themselves can be specialized by the model object they represent. My links usually do not have models associated with them, so I specialize them with the Java Void class. As you start using generics, you will notice that your code will become a lot less verbose. This is because most of your casts are redundant. Eclipse will point those out to you?and will even remove them if you use the code clean-up feature.

For some reason, the PropertyColumn on a data table is still not generic. A PropertyColumn is really useful when all you want to do is display an object’s property in a table. I am sure that the Wicket team will take care of this before 1.4 is released, but until then, you can create your own substitute class that works equally well, or borrow mine: ModelPropertyColumn (see Listing 1).

Leverage the Spring Support
Another notable feature of Wicket 1.4 is that it has built-in Spring support. This allows you to configure objects and services using Spring once, then use the Wicket Spring integration to inject these objects and services magically into your Wicket components. You will typically find yourself using Spring injection for data-access objects, but that is not the only use.

To start using Spring integration, add the wicket-spring dependency to your pom.xml, as follows:

  org.apache.wicket  wicket-spring  1.4-SNAPSHOT

Also, modify your web.xml to load up the Spring application context XML file when your Wicket web application starts up. Add the following lines to your web.xml:

  contextConfigLocation  /WEB-INF/applicationContext.xml      org.springframework.web.context.ContextLoaderListener  

Now, create the applicationContext.xml file in your WEB-INF directory. The applicationContext.xml file contains all the Spring beans that you want to create once for your web application.

Implement Dependency Injection
In order for Spring to inject dependencies into your web application, modify your web application’s init() method by adding the following line:

  addComponentInstantiationListener(new SpringComponentInjector(this)); 

This will force the Page (or anything derived from a Wicket component) to have its dependencies injected when created.

Now, you can introduce a member variable marked with a @SpringBean annotation in any page or component class, and this member variable will be initialized from the application context each time the page or component class is instantiated. Unlike injections at the doctor’s office, this one is painless.

Occasionally, I need to use the application context outside of a Wicket component, for example in a Wicket session. Digging deep into the Wicket code, I found out how to use Spring beans in my Wicket session. In my web application class, I have the following method:

  public ApplicationContext getApplicationContext()  {    return WebApplicationContextUtils      .getRequiredWebApplicationContext(getServletContext());  }

Now, I can use this method from my session, or from almost anywhere else, like so:

ApplicationContext applicationContext = ((WicketLearningApplication) Application.get()).getApplicationContext();

Congratulations! You have ventured on to the bleeding edge by using the nightly snapshot. Your web application will be in a great position to use Wicket 1.4 when it is released?and with a lot less code.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: