Tuesday, September 11, 2012

Wicket: Steps to Create Hello World Application on Eclipse (JUNO).

Go to File àNew à Dynamic Web Project.
Enter project name as ‘wicket’ and click on next.
Again click on next.
Now select ‘Generate web.xml deployment descriptor’ and click Finish.
 Now copy log4j-1.2.16.jar, slf4j-api-1.6.1.jar, slf4j-log4j12-1.6.1.jar, wicket-core-1.5.7.jar, wicket-request-1.5.7.jar and wicket-util-1.5.7.jar into WEB-INF/lib folder.
Now create new java file named HelloWorld.java extending WebPage in com.lnn.page package.
Add greeting label to HelloWorld.java
package com.lnn.page;

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

public class HelloWorld extends WebPage {

   private static final long serialVersionUID = -1674472041276604968L;
   public HelloWorld(){
         add(new Label("greeting", "Hello, World!"));
   }
}

Create the HelloWorld.html file in com.lnn.page package. (same name as webpage file).
Now add header tag with greeting wicket id. (Here I have added wicket name space html tag for convenience).
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
   xmlns:wicket="http://wicket.apache.org">
<body>
   <h1 wicket:id="greeting">This is greeting from web page.</h1>
</body>
</html>
     Create HelloWorldApplication by extending WebApplication in com.lnn.app package.
Return HelloWorld.class in getHomePage method.
package com.lnn.app;

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

import com.lnn.page.HelloWorld;

public class HelloWorldApplication extends WebApplication {
   @Override
   public Class<? extends Page> getHomePage() {
         // TODO Auto-generated method stub
         return HelloWorld.class;
   }
}

Now map the WicketFilter along applicationName initParam in web.xml.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>wicket</display-name>
  <filter>
    <filter-name>Application</filter-name>
    <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
    <init-param>
      <param-name>applicationClassName</param-name>
      <param-value>com.lnn.app.HelloWorldApplication</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>Application</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
Right click on the project Go to àRun As’ à ‘Run on Server’ and click on Finish.
We will see the following window after Finish.

1 comment: