The second type of control listener binding uses the ActionListener interface to provide compile time safety. This compile time binding also supports code refactoring using modern IDE tools.
public class ControlListenerType2Page extends Page { private ActionLink myLink = new ActionLink("myLink"); // Constructor ------------------------------------------------------------ /** * Create a new Page instance. */ public ControlListenerType2Page() { addControl(myLink); myLink.setActionListener(new ActionListener() { public boolean onAction(Control control) { String msg = "ControlListenerPage#" + hashCode() + " object method <tt>onAction()</tt> invoked."; addModel("msg", msg); return true; } }); } }
In the Page class we create an ActionLink called
myLink
. In the Page constructor we set the control's
action listener to an anonymous inner class which implements the method
onAction()
. When a user clicks on the
myLink
control it will invoke the action listener method
onAction()
.
As with our previous example, in the page template we define a 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 onAction()
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 onAction() invoked.