Wicket 6: Hit counter


This is part 6 of a series about Wicket.

Here’s the simplest way to keep track of the hits of a page.

This is Hello.html

<html>
  <body>
    <h2 wicket:id="message"></h2>
  </body>
</html>

And this is Hello.java

public class Hello extends WebPage {
   public Hello() {
      add(new Label("message", "Hello!"));
   }
}

Every page is a real object of type WebPage. So we may use a static field.

public class Hello extends WebPage {
   private static Integer counter = 1;
   public Hello() {
      add(new Label("message", "hits: " + counter++));
   }
}

Deploy it and hit the refresh button several times!

Close the browser window and open a new one. Is the value of hits preserved? Why?

Review

A simple solution implemented using simple Java code.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: