Click is an open source project, licensed using the Apache license.
Click uses the event based programming model for processing Servlet requests and Velocity for rendering the response.
This framework uses a single servlet, called ClickServlet, to act as a request dispatcher. When a request arrives ClickServlet creates a Page object to processes the request and then uses the page's Velocity template to render the results.
Page provide a simple thread safe programming environment, with a new page instance is created for each servlet request.
Possibly the best way to see how Click works is to dive right in and look an examples.
<click-app> <pages> <page path="hello-world.htm" classname="examples.page.HelloWorld"/> </pages> </click-app>Next we have our page class examples.page.HelloWorld:
package examples.page; import java.util.Date; import net.sf.click.Page; public HelloWorld extends Page { /** * @see Page#onGet() */ public void onGet() { addModel("time", new Date()); } }And then we have the page template hello-world.htm:
<html>
<body>
<h2>Hello World</h2>
Hello world from Click at $time
</body>
</html>
At runtime the ClickSerlvet maps a GET hello-world.htm
request to our page class example.page.HelloWorld and creates a new instance.
The ClickServlet then invokes the page's onGet() method. This method creates a
new Date object and adds it to the page's model as a value called
time.
The page model is then merged with the template which substitutes the
$time parameter with the Date object.
Velocity then renders the merged template which looks something like
Hello WorldHello world from Click at Wed May 28 15:52:29 EST 2005 |
public class Login extends Page { private Form form = new Form("form"); /** * @see Page#onInit() */ public void onInit() { addControl(form); form.add(new TextField("username", true)); form.add(new PasswordField("password", true)); form.add(new Submit("ok", " OK ", this, "onOkClicked")); form.add(new Submit("cancel", this, "onCancelClicked")); } public boolean onOkClicked() { if (form.isValid()) { User user = new User(); form.copyTo(user); if (UserDAO.isAuthenticatedUser(user)) { getContext().setSessionAttribute("user", user); setRedirect("secure.htm"); } else { form.setError(getMessage("authentication-error")); } } return true; } public boolean onCancelClicked() { setRedirect("index.htm"); return true; } }Next we have the Login page template login.htm:
<html>
<body>
<h2>Login</h2>
$form
</body>
</html>
When the Login page is first requested the $form object will
automatically render itself as:
Login
|
Login
|