Wicket Hello World Example

A simple hello world example in Wicket, show the basic structure of Wicket web application.

Tools and technologies used in this article

  1. Apache Wicket 1.4.17
  2. Eclipse 3.6
  3. Maven 3.0.3
  4. JDK 1.6.0.13

1. Directory Structure

See the final directory structure of this Wicket hello world web application. In Wicket, you need to put all files “.html” and “.java” in a same package directory.

Note
Read this control where HTML is loaded in Wicket article to learn how to separate the “.html” and “.java” in different directory.

See figure below :

wicket directory structure

Follow below steps to create the entire directory structure.

2. Maven Quick Start

Create a simple web application via Maven.


mvn archetype:generate -DgroupId=com.mkyong.core -DartifactId=WicketExamples 
 -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Now, it will create all the standard web folder structure.

3. Wicket Dependency

Add Wicket dependency in your Maven pom.xml file.

File : pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
	http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mkyong.core</groupId>
	<artifactId>WicketExamples</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>WicketExamples</name>
	<url>http://maven.apache.org</url>

	<dependencies>

		<dependency>
			<groupId>org.apache.wicket</groupId>
			<artifactId>wicket</artifactId>
			<version>1.4.17</version>
		</dependency>

		<!-- slf4j-log4j -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.5.6</version>
		</dependency>

	</dependencies>

	<build>
		<finalName>WicketExamples</finalName>

                <resources>
			<resource>
				<directory>src/main/resources</directory>
			</resource>
			<resource>
				<filtering>false</filtering>
				<directory>src/main/java</directory>
				<includes>
					<include>*</include>
				</includes>
				<excludes>
					<exclude>**/*.java</exclude>
				</excludes>
			</resource>
		</resources>

		<plugins>
			<plugin>
				<inherited>true</inherited>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<optimise>true</optimise>
					<debug>true</debug>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
Wicket need SLF4J !
You have to include the slf4j logging implementation, otherwise Wicket will be failed to start.
Wicket need resource filter
Remember to add the resource filter, Wicket puts all files in same package folder, if you didn’t define the resource filter to include everything “<include>*</include>” , “html”, “properties” or other resources files may failed to copy to the correct target folder.

4. Wicket Applications

In Wicket, most stuffs are work by convention, you don’t need to configure it. In this case, WebApplication returns a “Hello.class” as the default page, when Wicket see this “Hello.class“, it know the mark up “html” page should be “Hello.html“, and it should be able to find at the same package directory. This is why Wicket need you to put both “html” and “java” class together.

File : MyApplication.java – The main application entrance.


package com.mkyong;

import org.apache.wicket.Page;
import org.apache.wicket.protocol.http.WebApplication;
import com.mkyong.hello.Hello;

public class MyApplication extends WebApplication {

	@Override
	public Class<? extends Page> getHomePage() {
		return Hello.class; //return default page
	}

}

File : Hello.java


package com.mkyong.hello;

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

public class Hello extends WebPage {

	private static final long serialVersionUID = 1L;

    public Hello(final PageParameters parameters) {

        add(new Label("message", "Hello World, Wicket"));

    }
}

File : Hello.html


<html>
    <head>
        <title>Wicket Hello World</title>
    </head>
    <body>
	  <h1>
        <span wicket:id="message">message will be replace later</span>
	  </h1>
    </body>
</html>

5. Wicket Filters

To make Wicket works, you need to register Wicket filters in your web.xml file.

File : web.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
	version="2.4">
	<display-name>Wicket Web Application</display-name>

	<filter>
		<filter-name>wicket.wicketTest</filter-name>
		<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
		<init-param>
			<param-name>applicationClassName</param-name>
			<param-value>com.mkyong.MyApplication</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>wicket.wicketTest</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

</web-app>

6. Build it

All files are ready, build it with Maven.


mvn eclipse:eclipse -Dwtpversion=2.0

Import it into Eclipse and start the project.

7. Test It

Visit http://localhost:8080/WicketExamples/ , see figure :

Wicket hello world

Done.

Download it – Wicket-HelloWorld-Examples.zip (6KB)
Wicket Examples
You may interest to set up wicket example in your local environment to explore more about wicket components.

Reference

  1. Wicket Official site

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
9 Comment threads
7 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
13 Comment authors
Faisal BashirAhmedDiegosrinivasa raoSanjay Recent comment authors
newest oldest most voted
Insolven?ní Rejst?ík
Guest
Insolven?ní Rejst?ík

Nice one.
Maybe you could create a quickstart archetype and put it to some repo.

Nickie Bruster
Guest
Nickie Bruster

have been visiting your site for a few days. really love your posts. btw i am doing a study concerning this area. do you happen to know other great websites or online forums that I can get more information? thanks a ton.

srinivasa rao
Guest
srinivasa rao

Hi,

Thanks for your posts, these are very helpful to the beginners.
I am facing a problem while practicing this example. This example works some times and doesn’t some times. Some times i am getting with the following exception.

WicketMessage: Markup of type 'html' for component 'com.wicket.hello.Hello' not found. Enable debug messages for org.apache.wicket.util.resource to get a list of all filenames tried.: [Page class = com.wicket.hello.Hello, id = 2, version = 0]

I read the reason to get this exception in your another post as resources, but i couldn’t understand properly. Can you please let me know what exactly the reason behind error?

Thanks,
Srinivasa Rao.

Diego
Guest
Diego

With the provided pom-xml Hello.html is not copied by maven under /classes/com/mkyong/hello folder, the correct pom.xml portion is:

    <resources>
        <resource>
            <directory>src/main/java</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>

(no ‘filtering’ stuff)

KR
Diego

Sanjay
Guest
Sanjay

Hello Sir,

Please tell me how to start application
I have download and imported in the eclipse , but i dont know to to start server and run

Please guide me

thanks

Re,
sanjay

Diego
Guest
Diego

just execute
> mvn install
and copy the WicketExamples.war file into Tomcat or JBoss ‘deploy’ dir

Eyal
Guest
Eyal

Regarding LP comment –
Where is the html file to be changed?
Does the change from * to ** work?
Running the command of part 2 yielded just part of the directory structure shown in part 1.

Besides,
I wish to practice this tutorial at my personal computer at home.
Should I install Apache Wicket 1.4.17 ? or, I can I know that my Eclipse contains this framework?
Should I also install a server application or it is not necessary in order to learn this tutorial?

Thanks in advance,
Eyal

LP
Guest
LP
 
				<directory>src/main/java</directory>
				<includes>
					<include>*</include>
				</includes>

Shouldn’t the include be “**” or similar, as this include only includes the main level folder? I struggled with this for a while.

Ahmed
Guest
Ahmed

Thats right… it worked with me only when i put 2 * there! Thank you by the way

Nikola
Guest
Nikola

How to run this tutorial?

trackback
Wicket Tutorials

[…] Wicket Hello World Example A simple hello world to explore the basic structure of Wicket web application. […]

Pierluigi
Guest
Pierluigi

Hej,
thanks for the very nice and clear example.
I have a question I hope you might answer. If I have to compile the same source code and generate “two” applications (that are actually the same application) running with two different “pattern-url”, how should I modify this example? Is it possible to do what I’m asking for?
Many thanks,
Pierluigi