October 06, 2014

Steps to Create 'Wicket' Hello World Application on Eclipse

Open eclipse, select New  > Dynamic Web Project

Enter the project name as Hello World  and then click Next 


Select ‘Generate web.xml deployment descriptor’ and click Finish.
Now copy these jars into WEB-INF/lib folder:
  • 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 
  • wicket-util-1.5.7.jar

Now create new java file named HelloWorld.java in com.test.page package. This class will exend WebPage .

Inside HelloWorld.java  add  label msg

package com.test.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("msg", "Hello, World!"));
  }
}


Now we will create a html page with the same name as HelloWorld inside the same package as HelloWorld.java. 

HelloWorld.html is created in com.test.page package.

< ?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="msg" > This is greeting from web page. < /h1 >
< /body >
< /html >  

After this create a HelloWorldApplication.java inside com.test.app package by extending WebApplication.

package com.test.app;

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

import com.test.page.HelloWorld;

public class HelloWorldApplication extends WebApplication {
    @Override
    public Class getHomePage() {
        return HelloWorld.class;
    }
}


Now open web.xml and add entry for org.apache.wicket.protocol.http.WicketFilter

 < ?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5" > 
   < display-name >  HelloWorld1 < /display-name > 
   < welcome-file-list > 
     < welcome-file >  index.html < /welcome-file > 
     < welcome-file >  index.htm < /welcome-file > 
     < welcome-file >  index.jsp < /welcome-file > 
     < welcome-file >  default.html < /welcome-file > 
     < welcome-file >  default.htm < /welcome-file > 
     < welcome-file >  default.jsp < /welcome-file > 
   < /welcome-file-list > 
   < 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 > 


Last step, right click on the project > Run As > Run on Server and click on Finish.

-K Himaanshu Shukla..

No comments:

Post a Comment