| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>1.3. Control Listener Type 2 Example</title><link rel="stylesheet" href="css/stylesheet.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.75.0"><link rel="home" href="index.html" title="Apache Click"><link rel="up" href="ch01.html" title="Chapter 1. Introduction to Apache Click"><link rel="prev" href="ch01s02.html" title="1.2. Control Listener Type 1 Example"><link rel="next" href="ch01s04.html" title="1.4. Simple Table Example"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">1.3. Control Listener Type 2 Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch01s02.html">Prev</a> </td><th width="60%" align="center">Chapter 1. Introduction to Apache Click</th><td width="20%" align="right"> <a accesskey="n" href="ch01s04.html">Next</a></td></tr></table><hr></div><div class="sect1" title="1.3. Control Listener Type 2 Example"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="control-listener-type-2"></a>1.3. Control Listener Type 2 Example</h2></div></div></div><p>The second type of control listener binding uses the |
| <a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/control/ActionListener.html" target="_blank">ActionListener</a> |
| interface to provide compile time safety. This compile time binding also |
| supports code refactoring using modern IDE tools. |
| </p><pre class="programlisting"><span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">public</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">class</span> ControlListenerType2Page <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">extends</span> Page { |
| |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">private</span> ActionLink myLink = <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">new</span> ActionLink(<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="str">"myLink"</span>); |
| |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="com">// Constructor ------------------------------------------------------------</span> |
| |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="com">/** |
| * Create a new Page instance. |
| */</span> |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">public</span> ControlListenerType2Page() { |
| addControl(myLink); |
| |
| myLink.setActionListener(<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">new</span> ActionListener() { |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">public</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">boolean</span> onAction(Control control) { |
| String msg = <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="str">"ControlListenerPage#"</span> + hashCode() |
| + <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="str">" object method <tt>onAction()</tt> invoked."</span>; |
| addModel(<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="str">"msg"</span>, msg); |
| |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">return</span> true; |
| } |
| }); |
| } |
| |
| }</pre><p>In the Page class we create an ActionLink called |
| <code class="varname">myLink</code>. In the Page constructor we set the control's |
| action listener to an anonymous inner class which implements the method |
| <code class="methodname">onAction()</code>. When a user clicks on the |
| <code class="varname">myLink</code> control it will invoke the action listener method |
| <code class="methodname">onAction()</code>. |
| </p><p>As with our previous example, in the page template we define a HTML link and |
| have the <code class="varname">myLink</code> control render the link's href attribute: |
| </p><pre class="programlisting"><span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag"><html></span> |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag"><head></span> |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag"><link</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="atn">type</span>=<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="pln">"text/css"</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="atn">rel</span>=<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="pln">"stylesheet"</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="atn">href</span>=<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="pln">"style.css"</span><span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag">></span><span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag"></link></span> |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag"></head></span> |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag"><body></span> |
| |
| Click myLink control <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag"><a</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="atn">href</span>=<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="pln">"</span><code class="varname">$myLink.href</code>">here<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag"></a></span>. |
| |
| <span class="command"><strong>#if</strong></span> (<code class="varname">$msg</code>) |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag"><div</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="atn">id</span>=<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="pln">"msgDiv"</span><span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag">></span> <code class="varname">$msg</code> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag"></div></span> |
| <span class="command"><strong>#end</strong></span> |
| |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag"></body></span> |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag"></html></span></pre><p>At runtime this page would be rendered as:</p><div class="literallayout"><p>Click myLink control <code class="varname">here</code>.</p></div><p>When a user clicks on the link the <code class="methodname">onAction()</code> |
| method is invoked. This method then creates the <code class="varname">msg</code> model |
| value, which is rendered in the page as: |
| </p><div class="literallayout"><p>Click myLink control <code class="varname">here</code>.<br> |
| <br> |
| <code class="computeroutput">ControlListenerPage#12767107 object method onAction() invoked.</code></p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch01s02.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch01.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch01s04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">1.2. Control Listener Type 1 Example </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 1.4. Simple Table Example</td></tr></table></div></body></html> |