Hello World!

HelloWorld demonstrates the basic structure of a web application in Wicket. A Label component is used to display a message on the home page for the application.

HelloWorld

HelloWorldApplication.java

Each Wicket application is defined by an Application object. This object defines what the home page is, and allows for some configuration.

package wicket.examples.helloworld;

import wicket.protocol.http.WebApplication;

public class HelloWorldApplication extends WebApplication
{
    public HelloWorldApplication()
    {
        getPages().setHomePage(HelloWorld.class);
    }
}

Here you can see that we define wicket.examples.helloworld.HelloWorld to be our home page. When the base URL of our application is requested, the markup rendered by the HelloWorld page is returned.

HelloWorld.java

package wicket.examples.helloworld;

import wicket.markup.html.WebPage;
import wicket.markup.html.basic.Label;

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

The Label is constructed using two parameters:

  • "message"
  • "Hello World!"
The first parameter is the component identifier, which Wicket uses to identify the Label component in your HTML markup. The second parameter is the message which the Label should render.

HelloWorld.html

The HTML file that defines our Hello World functionality is as follows:

<html>
<body>
    <span wicket:id="message">Message goes here</span>
</body>
</html>

In this file, you see two elements that need some attention:

  • the component declaration <span wicket:id="message">
  • the message Message goes here
The component declaration consists of the Wicket identifier wicket and the component identifier message. The component identifier should be the same as the name of the component you defined in your WebPage. The message between the <span> tags is removed when the component renders its message. The final content of the component is determined by your Java code.

web.xml

In order to deploy our HelloWorld program, we need to make our application known to the application server by means of the web.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
      PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
      "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
    <display-name>Wicket Examples</display-name>
    <servlet>
        <servlet-name>HelloWorldApplication</servlet-name>
        <servlet-class>wicket.protocol.http.WicketServlet</servlet-class>
        <init-param>
          <param-name>applicationClassName</param-name>
          <param-value>wicket.examples.helloworld.HelloWorldApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloWorldApplication</servlet-name>
        <url-pattern>/helloworld</url-pattern>
    </servlet-mapping>
</web-app>

In this definition you see the Wicket servlet defined, which handles all requests. In order to let Wicket know which application is available, only the applicationClassName servlet parameter is needed.

Ready to deploy

That's it. No more configuration necessary! All you need to do now is to deploy the web application into your favourite application server.

As you can see: no superfluous XML configuration files are needed to enable a Wicket application. Only the markup (HTML) files, the Java class files and the required web.xml were needed to create this application.