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
- Apache Wicket 1.4.17
- Eclipse 3.6
- Maven 3.0.3
- 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.
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 :

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>
You have to include the slf4j logging implementation, otherwise Wicket will be failed to start.
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 :

Done.
You may interest to set up wicket example in your local environment to explore more about wicket components.
How to run this tutorial?
[...] Wicket Hello World Example A simple hello world to explore the basic structure of Wicket web application. [...]
Nice one.
Maybe you could create a quickstart archetype and put it to some repo.
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.
Info about what?
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
Sorry, i’m not really sure what you mean by two different URL-Pattern…
Two different “pattern-url”s are point to different application class
–> Suggest you create two projects for easy maintain
Two different “pattern-url”s are point to same application class
–> not really sure what u means by it, did you means following scenario?
http://localhost:8080/HelloWorld-1.0/
http://localhost:8080/HelloWorld-2.0/
point to the same Wicket application class?
If yes, then we can do it in server level with some redirect work to make it done, it’s not really a Wicket’s task.