Apache Wicket Examples

Basic examples

Hello, World! Because every examples site needs one

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.

Java code

The page extends WebPage, making it a component with associated markup: next to the class file, Wicket expects a markup file. In this case, Wicket will look for a file named HelloWorldPage.html, on the class path in the same Java package as the class: org.apache.wicket.examples.basic.helloworld.

In the constructor of the page, you add a label component to the component hierarchy. The component expects a component identifier and a text to display. In this case, the component identifier is msg and the display text is Hello, World!.

package org.apache.wicket.examples.basic.helloworld;

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

public class HelloWorldPage extends WebPage {
    private static final long serialVersionUID = 1L;

    public HelloWorldPage() {
        add(new Label("msg", "Hello, World!"));
    }
}

HTML Markup

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Hello, World!</title>
</head>
<body>
    <h1 wicket:id="msg">text goes here</h1>
</body>
</html>

This is a plain HTML 5 web page, with a h1 tag where the Hello, World! message should be rendered. By adding the wicket:id attribute to this h1 tag you tell Wicket where to let the label component do its work. In this case: replace the text between the tags with Hello, World!

The HTML file must have the same name as the Java class. So if our Java class is called HelloWorldPage then our markup file must be named HelloWorldPage.html, and reside in the same package as the Java class.

Tip: properly case your file name

As Java is case-sensitive you have to name your markup file in the exact same case. You might not notice it on case-insensitive file systems used by e.g. Windows, but when deployed in a WAR, case matters.

Browser output

x
-
+
Safari