This article supporting source code
Overview
Wicket is the Java WEB development framework that was recently enabled. It is an open source, lightweight, component-based framework that lets Wicket quickly stand out from the common methods of developing WEB applications. Wicket seeks to clearly define the role lines between HTML page designers and Java developers by supporting templates based on pure HTML, which can be built with any WYSIWYG HTML design tool and can be dynamically characterized by a slight modification.
Like other frameworks, Wicket is built on the servlet API of Sun Microsystems. However, unlike other frameworks based on the Model-view-controller (MVC) model, such as Struts, Wicket allows you to escape from the task of processing request/response objects, which are inherent in technologies such as servlet. With these tasks removed, Wicket lets you focus more on your application's business logic.
As a Wicket developer, you should consider building stateful reusable components rather than building a controller to handle request/response objects and also worrying about multithreading issues. Instead of building a controller or action class, you create a page, place components on the page, and then define how each component responds to user input.
HelloWorld sample
To really show the simplicity of using Wicket to develop web-based applications, you might want to develop a simple "Hello world" example. Developing a dynamic page in Wicket usually involves only creating the following two artifacts:
HTML templates
Java page Classes
Note: You must ensure that the actual HTML file and page class names are the same (for example, helloworld.html and Helloworld.java) and both are in the same position on the CLASSPATH. It is also best to place the two in the same directory.
HTML Template (helloworld.html)
The template file for the HelloWorld sample is shown in Listing 1.
Listing 1. Helloworld.html
<body bgcolor="#FFCC00">
<H1 align="center">
<span wicket:id="message">Hello World Using Wicket!</span>
</H1>
</body>
To make a Dynamic Web page, you need to determine the dynamic part of the page and tell wicket to use the component to render these parts. In Listing 1, I wanted to get a dynamic message, so I used the span element to mark the component, using the Wicket:id attribute to label the component.
Java Page class (Helloworld.java)
Listing 2 shows the page class for the Helloworld.java sample.
Listing 2. Helloworld.java
package myPackage;
import wicket.markup.html.WebPage;
import wicket.markup.html.basic.Label;
public class HelloWorld extends WebPage
{
public HelloWorld()
{
add(new Label("message", "Hello World using Wicket!!"));
}
}
The ID of the label component in the Page class ("message") must match the Wicket ID (wicket:id= "message") of this element in the template file. The Wicket Java Page class contains all the dynamic behavior of a Web page. There is a one-to-one relationship between the HTML template and the page class.
Finally, you need to create a Application object that is the starting point when the application is loaded by the Web container, and where it is set up and configured for application initialization. For example, you can define the application's home page by overwriting the Gethomepage () method and returning the page class corresponding to the application's home page, as shown in Listing 3.
Listing 3. Helloworldapplication.java
package myPackage;
import wicket.protocol.http.WebApplication;
public class HelloWorldApplication extends WebApplication {
protected void init() {
}
public Class getHomePage() {
return HelloWorld.class;
}
}