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>

7 comments:

  1. Nice write up though, but it looks like you have not been back here in a while. So, I thought I'd leave a comment just in case you decide to post again someday.

    I love you Wicket intro and hope you continue someday.

    ReplyDelete
  2. @Helen Thanks. This goes in my record books as my first blog comment. Really appreciate it. I will try to blog more often from now on. If you have anything u r particularly interested in let me know and I will blog about it. Thanks again

    ReplyDelete
  3. Yes, nice introduction! I'd like to recommend this ebook - it is also non-maven and covers many Wicket topics using Tomcat & Eclipse. The price is very fair as well.

    See:
    http://www.agileskills2.org/EWDW/index.html

    The first 2 chapters are free and will have you using forms.

    Valon

    ReplyDelete
  4. @Valon Sorry this is a little late. Thanks I will definitely check out the book.

    ReplyDelete
  5. hi this is bhavani shankar thanq very much this example is very useful for me please mail me(bhavanishankar466@gmail.com) if you have any material or examples or links regarding wicket so that it will be helpful for me

    ReplyDelete
  6. I have trouble in run my first wicket application .Please find the details below.

    a. I have created three files HelloWorldApplication.java,HelloWorld.java,HelloWorld.html,web.xml
    b. I have downloaded the wicket jar files and placed it in the tomcat lib directory
    c. I have set the classpath to the jar files
    d. Then I did the javac HelloWorld.java -> successfull
    e.Then I did javac HelloWorldApplication.java , the system throws the below error

    kumar@ubuntu:~/javaapplications/wicket$ javac HelloWorldApplication.java
    HelloWorldApplication.java:17: cannot find symbol
    symbol : class HelloWorld
    location: class HelloWorldApplication
    return HelloWorld.class;
    ^
    Note: HelloWorldApplication.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    1 error


    I need your help to run te application program , so that i can proceed to run my first wicket program in my machine.

    ReplyDelete
    Replies
    1. Hi Kumar,

      This tutorial was written long ago and was focused on Wicket 1.4. So I am not sure if the instructions are still valid. I am guessing that the issue you are facing has to do with the fact that the return type of getHomePage() is not using Generics. That is instead of Class use Class. This should stop the compiler from complaining. Another option would be to compile the file with a command such as javac -Xlint:unchecked HelloWorldApplication.java. Hope this helps.



      Delete