Detachable Models with Wicket (Attempt one)

Here is the Code. To run it, unzip it. You need Java and Maven. To run it and view it, just issue mvn jetty:run . Point your browser to http://localhost:8080 . You can also import it into Eclipse using File->Import->Existing Projects into Workspace and then select the directory where you unzipped it. This is built upon code created for the Wicket In Action book, although the example in the book (Chapter 4.3) for this area is just fragments that compile, yet don’t run.

The great thing about Wicket is that it stores the state on the server and can work with the back button. The bad thing about this is that the Wicket models you provide for your pages get serialized and stored in a cache. Create a successful website and you may be doomed to a lot of cache of repeated that was content fetched from the database. Wicket uses what is calls detachable models to counter this problem. This sample has the detachable model working, yet I am not sure it is doing its job. The good part is that it works and now it is just a process of seeing where the problem lies. Caching is supposed to work so that when state is saved, the objects contained in the LoadableDetachableModel are not serialized. At load time, it uses the load method. Download the code and look at the following areas.

First, I created the html for a list view.

Index.html

<div wicket:id="cheeses">
<h3 wicket:id="name">Gouda</h3>
<p wicket:id="description">Gouda is a Dutch...</p>
<p>
<span wicket:id="price">$1.99</span>
<a wicket:id="add" href="#">add to cart</a>
</p>
</div>

Then I created the Index.java that corresponds to the html.

Index.java

public class Index extends CheesrPage {
public Index() {

// LoadableDetachableModel<List<Cheese>>
CheeseDetach myDetach = new CheeseDetach(getDAO());
// ListView<Cheese> bound to "cheese" on html
CheeseList myCheeseList = new CheeseList("cheeses", myDetach, getCart());
// Add it to the page
add(myCheeseList);
[snip]
}

The java code above creates a LoadableDetachableModel called myDetach. It uses a CheeseDAO by calling getDAO() to construct it. Then, it creates a ListView called myCheeseList passing the myDetach (LoadableDetachableModel) to it. It finally adds it to the page.

The interface for CheeseDAO extends Serializable. I had to do this so it would not
produce errors.

public interface CheeseDAO extends java.io.Serializable {
public Cheese getCheese(Long id);
public List<Cheese> getCheeses();
}

This is troubling. It should not produce errors if it is not trying to serialize it. Ugh! Remove the extends java.io.Serializable from the interface, recompile and watch the output as it runs. It will throw an error (or is it an exception that recovers?) complaining that it is not serializable. Ugh again!

The bonus side is that I did get an LoadableDetachableModel working for a ListView. That was a little tricky! I may be complaining about nothing. Look at the following files from the code.

  • Index.java
  • CheeseDAO.java
  • CheeseDAOImpl.java
  • CheeseDetach.java
  • CheeseList.java

These files contain the essential details. I hope to gain more insight as to whether this really is not caching.

About Brian Lavender

I like to program in C++, Java, and Pascal. I have an admiration for languages like Pascal, Ada, and Eiffel. I work a lot with GNU/Linux systems.
This entry was posted in Wicket. Bookmark the permalink.