Using Apache Click with Codenvy

Create ApacheClick project off a Codenvy Factory:



Apache Click is a simple JEE web application framework for commercial Java developers. 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.

Create a Project

Log in to your Codenvy workspace and create a New WAR Project say “apacheClick”. Project file structure is shown below :

project_structure

Create pom.xml File and Specify Dependencies

Specify the below dependencies in pom.xml which are prerequisite for the apacheClick implementation. Make sure these jars are included under Maven Dependencies.

  • click-2.3.0.jar
  • click.extras.2.3.0.jar

Pom.xml


<name>apacheClick</name>
<build>
<finalName>apacheClick</finalName>
</build>
<dependencies>
<dependency>
<groupId>org.apache.click</groupId>
<artifactId>click</artifactId>
<version>2.3.0</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.apache.click</groupId>
<artifactId>click-extras</artifactId>
<version>2.3.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

Create FormPage class

This FormPage acts as a clickServlet and creates a page object with one input field and text field. When this servlet is processed this object is passed to a HTML page (form.htm) to render the processed result.

import org.apache.click.control.Form;
import org.apache.click.control.Submit;
import org.apache.click.control.TextField;
import org.apache.click.Page;

public class FormPage extends Page  {

    private static final long serialVersionUID = 1L;

    private Form form = new Form("form");

    // Constructor ------------------------------------------------------------

    public FormPage() {
        addControl(form);

        form.add(new TextField("name", true));
        form.add(new Submit("OK"));

        form.setListener(this, "onSubmit");
    }

    // Event Handlers ---------------------------------------------------------

    /**
     * Handle the form submit event.
     */
    public boolean onSubmit() {
        if (form.isValid()) {
            String msg = "Your name is " + form.getFieldValue("name");
            addModel("msg", msg);
        }
        return true;
    }

}

Create click.xml and web.xml

click.xml is smart enough to figure out that the FormPage class maps to the template form.htm. We only have to inform Click of the package of the FormPage class, in this case com.codenvy.FormPage.
click.xml:

<click-app charset="UTF-8">
<pages package="" autobinding="annotation">
<page path="form.htm" classname="com.codenvy.FormPage"></page>
</pages>
<mode value="profile"/>
</click-app>

Web.xml:

<web-app>
<display-name>apacheClick</display-name>

<welcome-file-list>
<welcome-file>form.htm</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>click-servlet</servlet-name>
<servlet-class>org.apache.click.ClickServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>click-servlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

</web-app>

Create form.htm

This html page is used to render the object processed through FormPage class.

<html>
  <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>MyFirstClick Welcome</title>  </head>
  <body>

    $form

    #if ($msg)
      <div id="msgDiv"> $msg </div>
    #end

  </body>
</html>

Build and Run the Application

Upon successful building the application and executing, it processes FormPage servlet and form object is passed to form.htm where we input a Name, upon clicking the OK button request again processed by servlet and displays the Name in the same form which has been passed as an object.

final_app

finalapp