Configure and build apache wicket without maven. A hello world example.

In my earlier tutorial, we have seen how to build a sample  wicket application with the help of mavan. In this tutorial, let us see how to configure and create a apache wicket hello world application in eclipse without using maven in easy steps

Tools used in this tutorial to configure apache-wicket project

1. Apache Wicket 6.16
2. Java : Java 1.6 or above
3. Eclipse with Apache server

Minimum Jars required used in this tutorial for starting this simple wicket project.

1. log4j-1.2.16.jar
2. slf4j-api-1.6.4.jar
3. slf4j-log4j12-1.6.4.jar
4. wicket-core-6.16.0.jar
5. wicket-request-6.16.0.jar
6. wicket-util-6.16.0.jar

#Your project may require more wicket jars depending upon your requirement to do more tasks.

Apache Wicket 6.16.0 can be downloaded from the apache site http://www.apache.org/dyn/closer.cgi/wicket/6.16.0. Unzip the downloaded apache-wicket-6.160.zip

Step 1: Open eclipse and create a dynamic web project using File->New->Dynamic Web Project
give the project name WicketTest and then click next…and finish

The initial directory structure in the eclipse will be as given below

wicket eclipse structure

Step 2: Get all the jars mentioned above from the unzipped folder and copy to the WEB-INF\lib folder…

Step 3: Replace the web.xml file located in the folder (\WEB-INF) with the below web.xml file.

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<display-name>wicketsample</display-name>
<filter>
<filter-name>wicket.wicketsample</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>com.javaonline.WicketStarter</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>wicket.wicketsample</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

The above web.xml is configured for the filter class (org.apache.wicket.protocol.http.WicketFilter) of any wicket application

Step 4 : Create Sample Java class and html files using wicket which are given below.

WicketStarter.java

package com.javaonline;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.protocol.http.WebApplication;

public class WicketStarter extends WebApplication
{
@Override
public Class<? extends WebPage> getHomePage()
{
return FirstPage.class;
}

@Override
public void init()
{
super.init();

}
}

FirstPage.java

package com.javaonline;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.request.mapper.parameter.PageParameters;

public class FirstPage extends WebPage {
Form<?> form = null;

public FirstPage(final PageParameters page) throws Exception {

form = new Form<Void>("testForm");

Label label=new Label("Label1","Select Me");

Link<?> wicket = new Link<Void>("apachewicket") {
@Override
public void onClick() {
PageParameters page = new PageParameters();
page.add("reqType", "Wicket");
try {
setResponsePage(new SecondPage(page));
} catch (Exception e) {
e.printStackTrace();
}
}

};

Link<?> hello = new Link<Void>("hello") {
@Override
public void onClick() {
PageParameters page = new PageParameters();
page.add("reqType", "HelloWorld");
try {
setResponsePage(new SecondPage(page));
} catch (Exception e) {
e.printStackTrace();
}
}

};

form.add(label);
form.add(wicket);
form.add(hello);
add(form);

}
}

FirstPage.html

<html>
<body>
<h3>Wicket Hello World example without Maven</h3>
<form wicket:id="testForm">
<span wicket:id="Label1"></span>
<li>&nbsp;<a href="#" wicket:id="apachewicket">Apache Wicket</a></li>
<li>&nbsp;<a href="#" wicket:id="hello">Hello World</a></li>

</form>
</body>
</html>

SecondPage.java

package com.javaonline;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;
public class SecondPage extends WebPage {

public SecondPage(PageParameters parameters) throws Exception
{
StringValue reqType = parameters.get("reqType");
System.out.println("reqType"+reqType);
if (reqType.toString().equals("Wicket"))
add(new Label("result", "You got to click Wicket"));
else if (reqType.toString().equals("HelloWorld"))
add(new Label("result", "Welcome to Apache Wicket Hello World"));
}
}

SecondPage.html

<html>
<body>
<span wicket:id="result"></span>
</body>
</html>

Step 5: Put all the above files under src\com\javaonline

Note :html file name should be same as the java file name.

Step 6: Include the src folder (WicketTest\src) in the class path.

Final directory structure of the web application created in this tutorial is given below.

wicket eclipse complete structure

Now run the project and get the below output

wicket home

 

Click on the link Hello World, you will get the below output page.

wicket second page

Reference:

Wicket start

wicket link

 

Leave a Reply