Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
Replies: 12 - Pages: 1  
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

Wicket: Using Links - An Example of Session State

At 12:15 AM on Jan 11, 2006, R.J. Lorimer wrote:

Welcome back to another article on Wicket . For those of you who haven't yet, please catch up on my entire Wicket series:

Wicket's rich server-side state model is a very powerful, and very different approach to managing a user-interaction with a Java web application - and because it is so different, it has the potential to have a lot of surprising and unexpected side-effects (and I mean this in a very positive way). To get started in understanding it, however, I'm just going to show you one of the most basic components in action - the wicket.markup.html.Link .

Links are a simple form of Wicket Component that listen for an event from user input, and when the event is triggered, a method on the component is called. There is an entire framework in Wicket for handling events and creating components that receive events; but that's out of the scope of this tip.

One of the most basic behaviors between any application and a user is to setup a 'push' style component (such as a button or a link) that the user may click (or push) to trigger some event. In the rich-client world (Swing, SWT, etc.) you create a component, the component renders itself, and the component (at its core) handles the user's event. In the web world, however - with most applications you have one Servlet/JSP that renders the link, and the link has some URL (usually defined by you) that calls another Servlet/JSP. Between time, the only thing you have to share state between the two requests is the data you put into that URL to call the other Servlet. Yes, you can put stuff in the session yourself, but it will be something you do for a single case-by-case basis.

Wicket brings you a *lot* closer to the rich-client world in the web world. Links are a great example of this. Why? Because, just like the above description for a rich-client application, you create the link component, the link component renders itself, and the link component responds to the user event.

Let's create a link on our AlohaPage from the previous tip. To create a simple link, we just need to create subclass of the abstract wicket.markup.html.Link class, and implement the abstract onClick() method. Usually we can do this using an anonymous inner class.

// a Link is a component, and requires an ID like most components.
new Link("[id goes here]") {
 public void onClick() {
  // handle link event here.
 }
}

Adding the link to our page is pretty much just as easy as seen above - we can add it to our page like any other component:

package com.javalobby.tnt.wicket.aloha;
 
import java.util.Date;
 
import wicket.markup.html.WebPage;
import wicket.markup.html.basic.Label;
import wicket.markup.html.link.Link;
 
public class AlohaPage extends WebPage {
	
	public AlohaPage() {
		// A Title for our page
		final Date firstVisitTime = new Date();
		add(new Label("pageTitle", "Aloha - The Time is: " + firstVisitTime));				
		add(new Link("dateLink") {
			@Override public void onClick() {
				System.out.println(
					"The user clicked the link at: " + 
					firstVisitTime + 
					"\nThe time now is :" + 
					new Date());
			}			
		});
		add(new PersonPanel("personPanel"));
	}
}

Putting it in the HTML markup is just as easy:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
<html 
	xmlns="http://www.w3.org/1999/xhtml" 
	xmlns:wicket="http://wicket.sourceforge.net/" 
	xml:lang="en" 
	lang="en">  
<head>
	<title wicket:id="pageTitle">[Page Title]</title>
</head>
<body onload="fromAlohaPage();">
	<h1>This is the BEGINNING of the Aloha Page</h1>
	
	<a href="#" wicket:id="dateLink">Click me!</a>
	<!-- 
		The new panel. Notice that it's just an empty tag that 
		will be filled in by the panel when it is asked to render.
	 -->
	<div wicket:id="personPanel"></div>
	
	<h1>This is the END of the Aloha Page</h1>
</body>
</html>

Note the addition of the 'Click me!' link under the header. Loading the page, here is what we see:

What's most interesting now is what happens when the user clicks the link. If I click it now, I am presented with this page:

Yes, that's right - I was taken back to the same page. That's because, by default, if an event is sent to the server and that event doesn't trigger a change to the response page property, the same page will be rendered. This is particularly easy for Wicket to do, because the page is still fully populated in the session.

If we look a little bit behind the scenes, we'll see something like this in the logs:

The user clicked the link at: Tue Jan 10 22:59:01 CST 2006
The time now is :Tue Jan 10 22:59:14 CST 2006

Let's go look at the link code again, and figure out what we're seeing. Two dates are in play here - the first is the Date that was created when the page object was created by wicket ( firstVisitTime ). The second is the one that we created when we handled the user response.

By looking at this, you can probably tell that I'm writing this article at 11:00 PM on January 10th, and that it took me around 13 seconds to click the link. What you can also recognize is that even after rendering the link to the user, Wicket was still able to hold on to the key bits of server-side state we needed to handle the response as if the user wasn't disconnected via the wide-world of TCP and the internet.

Notice that you didn't have to render the URL link yourself? This becomes a consistent theme in Wicket; links to pages and the URLs that compose them are something Wicket must control to properly handle server-side state and events properly. There is, of course, the concept of bookmarkable links, but that's a subject for a different tip :).

If we were to click the link again and again and again, you'll see that the same page (with the same first-visit time) answers our request each time:

The user clicked the link at: Tue Jan 10 22:59:01 CST 2006
The time now is :Tue Jan 10 22:59:14 CST 2006
The user clicked the link at: Tue Jan 10 22:59:01 CST 2006
The time now is :Tue Jan 10 23:02:18 CST 2006
The user clicked the link at: Tue Jan 10 22:59:01 CST 2006
The time now is :Tue Jan 10 23:03:01 CST 2006

So, in a nutshell, that's simple links in Wicket (Help, I'm in a nutshell! What kind of shell... has a nut like this?). There is so much more just lurking beneath the surface (or perhaps above the clouds is a better, more uplifting metaphor). Stay tuned!

Until next time,

R.J. Lorimer
Contributing Editor - rj -at- javalobby.org
Author              - http://www.coffee-bytes.com
Software Consultant - http://www.numbersix.com

1 . At 7:05 AM on Jan 11, 2006, Johan Compagner wrote:
  Click to reply to this thread Reply

Re: Wicket: Using Links - An Example of Session State

just one little correction.
That first time date is ofcourse not the first time the link was clicked. But the creation time of the link/page.

Some people are concerned about the state that is hold in the session but wicket tries to minimize that as much as possible and we also have pretty good clustering support (we are always thinking how a change/addition to the code would affect clustering).

Memory is pretty cheap nowadays.

The other side effect is security. Because if you push more data to the client that means that clients can see that data and alter it. Just remember links like this:

/account/remove.do?id=10


You can say: you should always encode id's. But are we all doing that by default? In wicket you just don't have to be bothered by this.
2 . At 8:45 AM on Jan 11, 2006, R.J. Lorimer wrote:
  Click to reply to this thread Reply

Re: Wicket: Using Links - An Example of Session State

Johan,

Thanks for the clarifying post. By firstTimeVisit I meant the first time the user visits the page, not the first time they clicked the link. I can see how it would be confusing though. I will try to update the article to account for that.

I'm hoping to spend more time in the near future showing how Wicket's approach to session state is worth the minor cost, and also how to minimize that cost by using models effectively. Once Wicket 1.2 releases of course, I could spend time talking about the eviction strategy, and if the 'stateless page' discussions come to a conclusion, that stuff too :)

Regards,
Best, R.J. Lorimer
3 . At 9:24 PM on Jan 11, 2006, Edward Wang wrote:
  Click to reply to this thread Reply

Re: Wicket: Using Links - An Example of Session State

It seems that every page refresh (clicking a link, etc.) needs to go back to the server. So how can AJAX thing comes into play?
4 . At 10:17 PM on Jan 11, 2006, R.J. Lorimer wrote:
  Click to reply to this thread Reply

Re: Wicket: Using Links - An Example of Session State

This is just one example of many regarding how links work in Wicket. Wicket does have AJAX support (which is getting a better definition in Wicket 1.2). We'll get there someday here on Javalobby, but in the mean time look up the AjaxHandler class.
Best, R.J. Lorimer
5 . At 10:37 PM on Jan 11, 2006, Jonathan Cone wrote:
  Click to reply to this thread Reply

Re: Wicket: Using Links - An Example of Session State

This stuff is solid gold - keep it coming.
6 . At 9:33 AM on Jan 12, 2006, Martijn Dashorst DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Wicket: Using Links - An Example of Session State

If you're in for an adventure, you can download the wicket-examples-1.2-20060108 distribution (a development release of 1.2), and take a look at the wicket.examples.ajax.prototype code , you can see our component rendering in action.

As for live AJAX: try our DOJO examples , which are currently developed by two interns at Topicus (the company I work for).
7 . At 10:13 AM on Jan 12, 2006, R.J. Lorimer wrote:
  Click to reply to this thread Reply

Re: Wicket: Using Links - An Example of Session State

Martin,

Thanks for amending this information. While I was aware of the DOJO examples (which will be really slick when they can be updated for Wicket 1.2), I wasn't aware of the AJAX Prototype - I'll have to take a look! This was some stuff I was trying to get working on my personal website re-write, but I got stuck on some of the component rendering; it looks like you guys took a similar/same approach but jumped through the extra hoops.

Thanks for sending the pointer!
Best, R.J. Lorimer
8 . At 11:07 AM on Jan 12, 2006, Jacek wrote:
  Click to reply to this thread Reply

Re: Wicket: Using Links - An Example of Session State

Out of curiousity...all these conponents shown on wicket-library.com...are they going to be opensource and part of Wicket, or will it be a commercial offering? thanks
9 . At 11:09 AM on Jan 12, 2006, Martijn Dashorst DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Wicket: Using Links - An Example of Session State

Be aware, it is still alpha work. However, I'm so excited, I put it into my actual commercial project just to WOW my collegues... It took out my weekend, but it was well worth the effort seeing the jaws dropping...

Also, for this to work, you really should be aligned with CVS HEAD. And CVS HEAD is known to be sometimes instable.

I'll try to come up with an article/example as soon as the technology is more mature. For now, use at your own risk!
10 . At 11:30 AM on Jan 12, 2006, Martijn Dashorst DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Wicket: Using Links - An Example of Session State

They are already open source and available through either CVS or . CVS may have better compatibility with Wicket 1.2.

You can download the components and the examples from
sourceforge . The main release site for the Dojo components can be found at http://wicket-stuff.sf.net/wicket-contrib-dojo .

Because my employer is a believer in Open Source development, we wanted the work of the students to become part of the community, and it was the premise of their assignment: design, develop and distribute open source Ajax components for the Wicket community.

They also have their own blog dedicated to the Wicket Dojo project .

The Wicket Examples at the live website are the deployed version of our examples distribution. I don't know precisely which version is deployed, but I guess it is the wicket-examples-1.1.zip distribution.
11 . At 11:53 AM on Jan 12, 2006, Jacek wrote:
  Click to reply to this thread Reply

Re: Wicket: Using Links - An Example of Session State

I agree. I've known about wicket before and I got all these examples up and running within an hour in NetBeans. If I remember the time it took me to just grasp JSP then it's quite impressive.

It seems there is a lot more in Wicket than this, definitely keep those tutorials coming! I am really liking this framework thus far...it just makes sense and the lack of config file entries is always a big plus.
12 . At 8:52 AM on Jan 15, 2006, Eelco Hillenius wrote:
  Click to reply to this thread Reply

Re: Wicket: Using Links - An Example of Session State

wicket-library ultimately can contain both open source and commercial offerings. I'm not sure how far Juergen is with that site.

We have a seperate project, Wicket Stuff that is meant for open source extensions and components for Wicket.

thread.rss_message