Chapter 1. Introduction to Apache Click

1.1. Hello World Example
1.2. Control Listener Type 1 Example
1.3. Control Listener Type 2 Example
1.4. Simple Table Example
1.5. Advanced Table Example
1.6. Simple Form Example
1.7. Advanced Form Example
1.7.1. Form Layout

Apache Click is a simple JEE web application framework for commercial Java developers.

Apache Click is an open source project, licensed under the Apache license .

Click uses an event based programming model for processing Servlet requests and Velocity for rendering the response. (Note other template engines such as JSP and Freemarker are also supported)

This framework uses a single servlet, called ClickServlet, to act as a request dispatcher. When a request arrives ClickServlet creates a Page object to process the request and then uses the page's Velocity template to render the results.

Pages provide a simple thread safe programming environment, with a new page instance created for each servlet request.

Possibly the best way to see how Click works is to dive right in and look at some examples. (The examples are also available online at http://click.avoka.com/click-examples/ under the menu "Intro Examples".)

1.1. Hello World Example

A Hello World example in Click would look something like this.

First we create a HelloWorld page class:

package examples.page;

import java.util.Date;
import org.apache.click.Page;

public HelloWorld extends Page {

    private Date time = new Date(); 1

    public HelloWorld() {
        addModel("time", time); 2
    }

}
1

Assign a new Date instance to the time variable.

2

Add the time variable to the Page model under the name "time". Click ensures all objects added to the Page model is automatically available in the Page template.

Next we have a page template hello-world.htm, where we can access the Page's time variable using the reference $time:

<html>
  <body>

    <h2>Hello World</h2>

    Hello world from Click at $time

  </body>
</html>

Click is smart enough to figure out that the HelloWorld page class maps to the template hello-world.htm. We only have to inform Click of the package of the HelloWorld class, in this case examples.page. We do that through the click.xml configuration file which allows Click to map hello-world.htm requests to the examples.page.HelloWorld page class.

<click-app>
  <pages package="examples.page"/>
</click-app>

At runtime the following sequence of events occur. The ClickSerlvet maps a GET hello-world.htm request to our page class example.page.HelloWorld and creates a new instance. The HelloWorld page creates a new private Date object, which is added to the page's model under the name time.

The page model is then merged with the template which substitutes the $time reference with the Date object. Velocity then renders the merged template as:

Hello World Screenshot

Figure 1.1. Hello World Screenshot