Hello World

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

In all the Wicket examples, you have to put all files in the same package directory. This means putting the markup files and the java files next to one another. It is possible to alter this behavior, but that is beyond the scope of this example. The only exception is the obligatory web.xml file which should reside in the WEB-INF/ directory of your web application root folder.

If you wish to start building this example, you may want to take a look at the Wicket Quickstart project, which provides a quick way of getting up and running without having to figure things out yourself. The Quickstart project contains the necessary build files for Apache Maven, libraries, minimal set of Java and markup files and an embedded Jetty server to run your application without having to go through the whole build-deploy cycle.

HelloWorldApplication.java

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

import org.apache.wicket.Page;
import org.apache.wicket.protocol.http.WebApplication;

public class HelloWorldApplication extends WebApplication
{
	/**
	 * @see org.apache.wicket.Application#getHomePage()
	 */
	@Override
	public Class<? extends Page> getHomePage()
	{
		return HelloWorld.class;
	}
}

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

HelloWorld.java

import org.apache.wicket.markup.html.WebPage;
import org.apache.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:

  1. “message”
  2. “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:

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wicket="http://wicket.apache.org">
<head>
    <title>Hello World</title>
</head>
<body>
    <span wicket:id="message">The real message goes here</span>
</body>
</html>

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

  1. the component declaration <span wicket:id=”message”>
  2. the text The real message goes here

The component declaration consists of the Wicket identifier wicket:id. The component identifier should be the same as the name of the component you defined in your WebPage. The text 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 application, 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"?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
		 version="2.5">

	<filter>
		<filter-name>HelloWorldApplication</filter-name>
		<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
		<init-param>
		  <param-name>applicationClassName</param-name>
		  <param-value>org.apache.wicket.reference.helloworld.HelloWorldApplication</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>HelloWorldApplication</filter-name>
		<url-pattern>/helloworld/*</url-pattern>
		<dispatcher>REQUEST</dispatcher>
		<dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
	</filter-mapping>

	<session-config>
		<session-timeout>5</session-timeout>
	</session-config>

</web-app>

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

Also, notice the url-mapping to /*. The Wicket filter will only process requests that are Wicket requests. If a request is not Wicket related, the filter will pass the request on to the chain. This ensures that (static) resources outside the realm of the Wicket application, such as style sheets, JavaScript files, images and so forth will be served by the container.

Ready to deploy

That’s it. No more configuration necessary! All you need to do now is to deploy the web application into your favorite application server. Point your browser to the url: http://<servername>/<warfilename>/, substituting servername and warfilename to the appropriate values, such as http://localhost:8080/helloworld/.

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.

Table Of Contents

Previous topic

Welcome to Apache Wicket’s documentation!

Next topic

The application class