Learn about Apache Click

Source: Internet
Author: User

Use lightweight models to quickly write WEB applications to avoid complex frameworks

The example in this article requires that you install Eclipse and the WEB application server. These examples use Tomcat 6.0, but other WEB application servers, such as the websphere®application server Community Edition, Jetty, or Tomcat, should also be available.

Apache Click Overview

The Apache Click is a Web application framework that you can use to quickly build Java Web applications. The goal of Apache Click is to provide a way to build Web applications without using a JSP or MVC framework, which simplifies the development of Web applications. Apache Click uses the Apache License license agreement.

The Apache Click does not use a complex framework, but rather uses HTML templates and normal legacy Java objects (POJO). HTML templates are processed by the Apache Velocity template engine, which makes the template as close to standard HTML as possible and does not require special markup. Using the Velocity template does not require learning complex syntax, allowing WEB developers to play their existing HTML skills.

POJO inherits the base class provided by the Apache Click release. Apache Click provides a page class and component classes that implement the processing code for each page by writing their subclasses. These components provide out-of-the-box functionality that you can use to build HTML controls and respond to events.

Apache Click also contains an API for mock testing that can be used to improve unit testing, continuous integration builds, and test-driven development (TDD).

Apache Click components and event handling

The Org.apache.click.Page class is the base class for all page components. You can use the methods in the page base class to handle a variety of events, such as page initialization (OnInit ()), page display (OnRender ()), and page security checks (Onsecuritycheck ()). In the classes that extend the Page, you can provide custom functionality by overriding these methods. Each class of the extended page can also contain methods for handling page events.

By implementing the Java.io.Serializable interface and calling the Setstateful () method of the page base class in the constructor, you can create a stateful page (see Listing 1).

Listing 1. To set the stateful property in a construction method

public WelcomePage() {
     setStateful(true);
   }

The Apache Click provides several control components that correspond to HTML elements. By dynamically adding these control components, you can build the form programmatically. By using these controls, you can programmatically get and set HTML control data in Java code.

By using the Seteventlistener () method of a field control (that is, TextField, Select), you can define a method on the class that extends the Page, and if the input in the field is valid, this method is executed during the form processing.

Buttons and links also provide event handling. You can create a new Submit button on the form and link it to a method in the Page class, as shown in Listing 2.

Listing 2. To create a submit button with the OnClick event-handling method

Submit Submitbutton = new Submit ("Submit Me", this, "OnClick");

When the button is clicked, the event is assigned to the OnClick () method of the custom Page class. Executes all the code in the OnClick () method, including the code that saves the data to the database or to another page.

Apache Click Template

By default, Apache Click uses the Apache Velocity template engine. Depending on some of the default mapping rules, the template automatically matches the class that extends the Page in the application. For example, a page class named Hellopage automatically matches the HTML file hello.html in a Web project. Page classes named Helloworldpage are automatically mapped to helloworld.html or hello-world.html files.

If you don't like the default page map, you can modify the Click.xml file to add your own custom mappings.

In addition to modifying the page mappings used by velocity, you can also decide not to use velocity as the template engine. Apache Click allows you to display pages using JSP, which helps you use existing JSPs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.