Wicket Hello World Example with Maven – Tutorial
Here i will demonstrates how to create a simple Wicket Hello World Example. The following Wicket java and xml files will be created at the end of the tutorial.
1) Index.java
2) Index.html
3) MyApplication.java
4) web.xml
5) pom.xml (optional, for maven project management and deployment)
1.) Create following wicket file structure.
mkyong
| pom.xml
|
\---src
+---main
| +---java
| | \---com
| | \---mkyong
| | Index.html
| | Index.java
| | MyApplication.java
| |
| \---webapp
| \---WEB-INF
| web.xml
2.) Wicket application class (MyApplication.java)
package com.mkyong; import org.apache.wicket.protocol.http.WebApplication; public class MyApplication extends WebApplication { public Class<Index> getHomePage() { return Index.class; } }
3.) Index html page and java file (Index.java, Index.html)
Index.java
package com.mkyong; import org.apache.wicket.PageParameters; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.WebPage; /** * Homepage */ public class Index extends WebPage { private static final long serialVersionUID = 1L; public Index(final PageParameters parameters) { // Add the simplest type of label add(new Label("message", "Wicket Hello World")); } }
Index.html
<html> <head> <title>Wicket Hello World</title> </head> <body> <br/><br/> <h1> <span wicket:id="message">message will be here, replace later</span> </h1> </body> </html>
4. Create a web.xml file for web deployment.
<?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>HelloWorld</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>
5.) Create a maven’s pom.xml for wicket dependencies download and deployment. Please access http://maven.apache.org/pom.html to know more about maven’s pom.xml file.
<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</groupId> <artifactId>HelloWorld</artifactId> <packaging>war</packaging> <version>1.0</version> <name>wicketHelloWorld</name> <dependencies> <!-- WICKET DEPENDENCIES --> <dependency> <groupId>org.apache.wicket</groupId> <artifactId>wicket</artifactId> <version>${wicket.version}</version> </dependency> <!-- LOGGING DEPENDENCIES - LOG4J --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.4.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> </dependencies> <build> <resources> <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.5</source> <target>1.5</target> <optimise>true</optimise> <debug>true</debug> </configuration> </plugin> </plugins> </build> <properties> <wicket.version>1.4-rc2</wicket.version> </properties> </project>
6.) Use Maven command (mvn install) to build the Wicket Hello World project and generate war file for deployment.
D:\mkyong>mvn install [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building wicketHelloWorld [INFO] task-segment: [install] [INFO] ------------------------------------------------------------------------ [INFO] [resources:resources] [INFO] Using default encoding to copy filtered resources. [INFO] [compiler:compile] [INFO] Compiling 1 source file to D:\mkyong\target\classes [INFO] [resources:testResources] [INFO] Using default encoding to copy filtered resources. [INFO] [compiler:testCompile] [INFO] No sources to compile [INFO] [surefire:test] [INFO] No tests to run. [INFO] [war:war] [INFO] Packaging webapp [INFO] Assembling webapp[HelloWorld] in [D:\mkyong\target\HelloWorld-1.0] [INFO] Processing war project [INFO] Webapp assembled in[188 msecs] [INFO] Building war: D:\mkyong\target\HelloWorld-1.0.war [INFO] [install:install] [INFO] Installing D:\mkyong\target\HelloWorld-1.0.war to D:\maven_repo\com\mkyong\HelloWor [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 7 seconds [INFO] Finished at: Thu Feb 19 10:01:56 SGT 2009 [INFO] Final Memory: 7M/16M [INFO] ------------------------------------------------------------------------
7.) Copy war file from target folder and deploy to web server like Tomcat.

Done we created a simple wicket hello world example with maven. Wicket is simple, neat and totally get rid of xml configuration file.
Download Wicket Hello World Example Source Code Here
Please set up a wicket example to explore more about wicket usage.How do setup Wicket Examples in Eclipse



I am starting to get totally distressed. I can not find a way to get the best shared web hosting for my internet websites. I am using ixwebhosting, but they are generally down every other day! Can anyone help me?
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.