Click includes a library of Controls which provide user interface functionality.
One of the commonly used controls is the ActionLink, which you can use to have an HTML link call a method on a Page object. For example:
public class ControlListenerType1Page extends Page { /* Set the listener to this object's "onLinkClick" method. */ private ActionLink myLink = new ActionLink("myLink", this, "onLinkClick") private String msg; // Constructor ------------------------------------------------------------ public ControlListenerType1Page() { addControl(myLink); } // Event Handlers --------------------------------------------------------- /** * Handle the ActionLink control click event. */ public boolean onLinkClick() { String msg = "ControlListenerPage#" + hashCode() + " object method <tt>onLinkClick()</tt> invoked."; addModel("msg", msg); return true; } }
Add the link to the page. The link will be made available to the
page template under the variable |
In the Page class we create an ActionLink called
myLink
and define the control's listener to be the page
method onLinkClick()
. When a user clicks on
myLink
control it will invoke the listener method
onLinkClick()
.
In Click a control listener method can have any name but it must return a boolean value. The boolean return value specifies whether processing of page events should continue. This control listener pattern provides a short hand way for wiring up action listener methods without having to define anonymous inner classes.
The advantage of this style of control listener binding is that you have to write fewer lines of code. The disadvantage of this type of control listener binding is that no compile time safety is provided, and you miss out on the compiler refactoring capabilities provided with modern IDEs.
Back to our example, in the page template we define an HTML link and
have the myLink
control render the link's href attribute:
<html> <head> <link type="text/css" rel="stylesheet" href="style.css"></link> </head> <body> Click myLink control <a href="$myLink.href
">here</a>. #if ($msg
) <div id="msgDiv">$msg
</div> #end </body> </html>
At runtime this page would be rendered as:
Click myLink control here
.
When a user clicks on the link the onLinkClick()
method is invoked. This method then creates the msg
model
value, which is rendered in the page as:
Click myLink control here
.
ControlListenerPage#12767107 object method onLinkClick() invoked.