Thursday, August 13, 2009

Hello World Wicket Development without the Maven burden

For most of us in Nigeria (this does not apply to the privileged few) learning a new technology nowadays is usually a challenge. Imagine a situation where I am trying to learn how to use a technology like Tapestry 5 or Wicket for the first time and having to pick up another technology like Maven. Don't get me wrong Maven is cool (hope the Ant people are not annoyed. Ant is cool too).

Okay I was in my little room feeling happy that I had just downloaded the Wicket 1.4 stable release using a free connection (I don't have one at home don't ask why) and I would like to try it out with its support for generics among others. I open up my Eclipse 3.4.0 and then I remember what happened when I first used Wicket 1.3 (refer to the Wicket Quickstart if you don't know what I am blabbing about). No Maven on this computer and no Internet. Damn. Hence this post was written to teach you how to use Wicket without relying on Maven. Read on if you are interested.

This post assumes that you are familiar with Java, Eclipse (with the Java EE perspective) and that you have downloaded Wicket 1.4. Your Eclipse environment should also have been setup to run on a server like Apache Tomcat 6.

These are the basic steps:
a) In your Eclipse IDE start a new Dynamic Web Project (name the project HelloWorldWicket)

b) Unzip your Wicket download to anywhere you like (for example, C:\apache-wicket-1.4.0)

c) Navigate to the lib folder under the the folder where you unzipped Wicket and copy the file named wicket-1.4.0.jar to the WEB-INF/lib of your Eclipse project.

d) Create 2 Java class files HelloWorldApplication.java and HelloWorld.java under the package helloworld (copy and paste the code for these files from this post)

e) Create an html file called HelloWorld.html and place it in the sample package folder as the HelloWorld.java (copy and paste the code for this file from this post)

f) Copy and paste the code for the web.xml file from this post and replace the WEB-INF/web.xml file in your eclipse project.

g) If you try to run the project now your will get an error java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory. The tricky part starts now

h) Navigate to the lib folder under the the folder where you unzipped Wicket. You will see a file called wicket-examples-1.4.0.war. Use the jar command to unzip the WEB/lib folder (for those that do not know the command is "<path_to_java_jdk_installation>/bin/jar -xvf <path_to_unzipped_wicket>/wicket-examples-1.4.0.war WEB-INF/lib" without the curly braces). The files we are interested in are called log4j-1.2.13.jar, slf4j-api-1.4.2.jar and slf4j-log4j12-1.4.2.jar. Copy these files to the WEB/lib of your Eclipse project and run the application. You should now see a nice Hello World by navigating your browser to http://localhost:8080/HelloWorldWicket/

Hope this post is useful. As I promised the code for this post is found below.


HelloWorldApplication.java
package helloworld;

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

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


HelloWorld.java
package helloworld;  

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


HelloWorld.html
<html>
<body>
<span wicket:id="message" id="message" />
</body>
</html>
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_2_5.xsd" id="HelloWorldWicket" version="2.5">
<display-name>HelloWorldWicket</display-name>
<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>helloworld.HelloWorldApplication</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>HelloWorldApplication</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

Monday, July 27, 2009

My First Post - Fun and Supposedly Meaningless

Thank God it seems I am finally able to post something. Awesome... I wasn't sure what it should be so I decided to practice with this somewhat meaningless writeup. Not much for my first post right.
Some of my posts may be tutorial-style while others may be just random babbling. When they do seem like random babbling take note that I have probably been overworked and I am probable just letting off some steam.
I am hoping that some of my posts will be useful to some people just as the posts of others have been useful to me (the 80-20 rule).
Have fun.....