"Hello World" example in Wicket

Hello World example is everybody's favorite example while starting any new technology. So in this Wicket tutorial we are going to develop first application in wicket which print "Hello World".

"Hello World" example in Wicket

"Hello World" example in Wicket

     

Hello World example is everybody's favorite example while starting any new technology. So in this Wicket tutorial we are going to develop first application in wicket which print "Hello World".

   In the previous example you have learned how to create a project in the Wicket using the Net Beans IDE. In this "HelloWorld" example we need to create following three files:

  1. HelloWorldApplication.java
  2. HelloWorld.java
  3. HelloWorld.html

One important thing to remember here is that you need to keep all these three files in a same folder. Here is the example code for HelloWorldApplication class. 

HelloWorldApplication.java

package com.roseindia.wicket;

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

public class HelloWorldApplication extends WebApplication
{
  public HelloWorldApplication()
  {

  }
 
  public Class getHomePage()
  {
  return HelloWorld.class;
  }
}

HelloWorldApplication contains one constructor and a function which returns class and this is the class where we have added the "Hello World!" message. Full code of  HelloWorld.java  is as follows:

HelloWorld.java

package com.roseindia.wicket;

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!"));
  }
}

This HelloWorld class output would be rendered on HTML file. The "wicket:id" finds the component corresponding to component identifier and will show component as output on the browser. You will notice that message written in the span will be replaced with the label defined in the HelloWorld class.

HelloWorld.html

 <html>
  <head>
  <title>Hello World</title>
   </head>
   <body>
  <span wicket:id="message" id="message">Message goes here</span>
   </body>
</html>

For HelloWorld example to run, we have to do corresponding entry of HelloWorldApplication class into the web.xml file. The entry that we need to do in the XML file is as follows:

web.xml

<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>
com.roseindia.wicket.HelloWorldApplicationn
 </param-value>
   </init-param>
   <init-param>
   <param-name>debug</param-name>
   <param-value>2</param-value>
  </init-param>
 </filter>
<filter-mapping>
   <filter-name>HelloWorldApplication</filter-name>
   <url-pattern>/wicket/hello/*</url-pattern>
</filter-mapping>

Output:

To run this example run WicketExample project and  type the following URL on the browser:

http://localhost:8080/WicketExample/wicket/hello

Download Source Code