6 min read

(For more resources related to this topic, see here.)

Wicket is a component-based Java web framework that uses just Java and HTML. Here, you will see the main advantages of using Apache Wicket in your projects.

Using Wicket, you will not have mutant HTML pages. Most of the Java web frameworks require the insertion of special syntax to the HTML code, making it more difficult for Web designers. On the other hand, Wicket adopts HTML templates by using a namespace that follows the XHTML standard. It consists of an id attribute in the Wicket namespace (wicket:id). You won’t need scripts to generate messy HTML code. Using Wicket, the code will be clearer, and refactoring and navigating within the code will be easier. Moreover, you can utilize any HTML editor to edit the HTML files, and web designers can work with little knowledge of Wicket in the presentation layer without worrying about business rules and other developer concerns.

The advantages for developers are as follows:

  • All code is written in Java
  • No XML configuration files
  • POJO-centric programming
  • No Back-button problems (that is, unexpected and undesirable results on clicking on the browser’s Back button)
  • Ease of creating bookmarkable pages
  • Great compile-time and runtime problem diagnosis
  • Easy testability of components

Another interesting thing is that concepts such as generics and anonymous subclasses are widely used in Wicket, leveraging the Java programming language to the max.

Wicket is based on components. A component is an object that interacts with other components and encapsulates a set of functionalities. Each component should be reusable, replaceable, extensible, encapsulated, and independent, and it does not have a specific context. Wicket provides all these principles to developers because it has been designed taking into account all of them. In particular, the most remarkable principle is reusability. Developers can create custom reusable components in a straightforward way. For instance, you could create a custom component called SearchPanel (by extending the Panel class, which is also a component) and use it in all your other Wicket projects. Wicket has many other interesting features. Wicket also aims to make the interaction of the stateful server-side Java programming language with the stateless HTTP protocol more natural.

Wicket’s code is safe by default. For instance, it does not encode state in URLs. Wicket is also efficient (for example, it is possible to do a tuning of page-state replication) and scalable (Wicket applications can easily work on a cluster).

Last, but not least, Wicket has support for frameworks like EJB and Spring.

Installation

In seven easy steps, you can build a Wicket “Hello World” application.

Step 1 – what do I need?

Before you start to use Apache Wicket 6, you will need to check if you have all of the required elements, listed as follows:

  • Wicket is a Java framework, so you need to have Java virtual machine (at least Version 6) installed on your machine.
  • Apache Maven is required. Maven is a tool that can be used for building and managing Java projects. Its main purpose is to make the development process easier and more structured. More information on how to install and configure Maven can be found at http://maven.apache.org.

The examples of this book use the Eclipse IDE Juno version, but you can also use other versions or other IDEs, such as NetBeans. In case you are using other versions, check the link for installing the plugins to the version you have; the remaining steps will be the same. In case of other IDEs, you will need to follow some tutorial to install other equivalent plugins or not use them at all.

Step 2 – installing the m2eclipse plugin

The steps for installing the m2eclipse plugin are as follows:

  1. Go to Help | Install New Software.
  2. Click on Add and type in m2eclipse in the Name field; copy and paste the link https://repository.sonatype.org/content/repositories/forge-sites/m2e/1.3.0/N/LATEST onto the Location field.
  3. Check all options and click on Next.
  4. Conclude the installation of the m2eclipse plugin by accepting all agreements and clicking on Finish.

Step 3 – creating a new Maven application

The steps for creating a new Maven application are as follows:

  1. Go to File | New | Project.
  2. Then go to Maven | Maven Project.
  3. Click on Next and type wicket in the next form.
  4. Choose the wicket-archetype-quickstart maven Archetype and click on Next.

  5. Fill the next form according to the following screenshot and click on Finish:

Step 4 – coding the “Hello World” program

In this step, we will build the famous “Hello World” program. The separation of concerns will be clear between HTML and Java code. In this example, and in most cases, each HTML file has a corresponding Java class (with the same name).

First, we will analyse the HTML template code. The content of the HomePage.html file must be replaced by the following code:

<!DOCTYPE html> <html > <body> <span wicket_id=”helloWorldMessage”>Test</span> </body> </html>


It is simple HTML code with the Wicket template wicket:id=”helloWorldMessage”. It indicates that in the Java code related to this page, a method will replace the message Test by another message.

Now, let’s edit the corresponding Java class; that is, HomePage.

package com.packtpub.wicket.hello_world; import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.markup.html.basic.Label; public class HomePage extends WebPage { public HomePage() { add(new Label(“helloWorldMessage”, “Hello world!!!”)); } }


The class HomePage extends WebPage; that is, it inherits some of the WebPage class’s methods and attributes, and it becomes a WebPage subtype. One of these inherited methods is the method add(), where a Label object can be passed as a parameter. A Label object can be built by passing two parameters: an identifier and a string. The method add() is called in the HomePage class’s constructor and will change the message in wicket:id=”helloWorldMessage” with Hello world!!!. The resulting HTML code will be as shown in the following code snippet:

<!DOCTYPE html> <html > <body> <span>Hello world!!!</span> </body> </html>


Step 5 – compile and run!

The steps to compile and run the project are as follows:

  1. To compile, right-click on the project and go to Run As | Maven install. Verify if the compilation was successful. If not, Wicket provides good error messages, so you can try to fix what is wrong.
  2. To run the project, right-click on the class Start and go to Run As | Java application. The class Start will run an embedded Jetty instance that will run the application. Verify if the server has started without any problems.
  3. Open a web browser and enter this in the address field: http://localhost:8080. In case you have changed the port, enter http://localhost:<port>.
  4. The browser should show Hello world!!!.

    The most common problem that can occur is that port 8080 is already in use. In this case, you can go into the Java Start class (found at src/test/java) and set another port by replacing 8080 in connector. setPort(8080) (line 21) by another number (for example, 9999).

  5. To stop the server, you can either click on Console and press any key or click on the red square on the console, which indicates termination.

And that’s it!

By this point, you should have a working Wicket “Hello World” application and are free to play around and discover more about it.

Summary

This article describes how to create a simple “Hello World” application using Apache Wicket 6.

Resources for Article :


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here