So, what is Apache Wicket?

Exclusive offer: get 50% off this eBook here
Instant Apache Wicket 6 [Instant]

Instant Apache Wicket 6 [Instant] — Save 50%

Learn how to get started with Apache Wicket 6 with this book and ebook

$14.99    $7.50
by Felipe Fedel Pinto João Sávio Ceregatti Longo | September 2013 | Open Source Web Development

This article by João Sávio Ceregatti Longo and Felipe Fedel Pinto, authors of the book Instant Apache Wicket 6, aims to introduce the reader to the world of Apache Wicket 6. This beginner-level guide takes the reader through the basic workflow of Apache Wicket in a practical and friendly style.

(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 xmlns:wicket="http://wicket.apache.org"> <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 xmlns:wicket="http://wicket.apache.org"> <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:


Instant Apache Wicket 6 [Instant] Learn how to get started with Apache Wicket 6 with this book and ebook
Published: August 2013
eBook Price: $14.99
See more
Select your format and quantity:

About the Author :


Felipe Fedel Pinto

Felipe Fedel Pinto holds a Bachelor's degree in Computer Science and has been developing and managing projects using Wicket since 2010. He also collaborates on open source Wicket projects. He is partner and Software Architect at Ampliato—http://www.ampliato.com.br.

João Sávio Ceregatti Longo

João Sávio Ceregatti Longo has a Bachelor's and Master's degree in Computer Science. He also has a strong background in the Java programming language (consultant, developer, evangelist, SCJP 6 certified) and is a Wicket enthusiast. He is currently working at Daitan Group—http://www.daitangroup.com.

Books From Packt


Apache Solr 3 Enterprise Search Server
Apache Solr 3 Enterprise Search Server

Instant OSGi Starter
Instant OSGi Starter

Performance Testing with JMeter 2.9
Performance Testing with JMeter 2.9

Liferay Portal Systems Development
Liferay Portal Systems Development

Sakai CLE Courseware Management: The Official Guide
Sakai CLE Courseware Management: The Official Guide

Apache Wicket Cookbook
Apache Wicket Cookbook

Instant Apache Solr for Indexing Data How-to
Instant Apache Solr for Indexing Data How-to

Apache Maven 3 Cookbook
Apache Maven 3 Cookbook


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
D
1
W
k
F
5
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software