blob: 1c7424589e6d971f1b17ef475690e318180a8afa [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>2.2.&nbsp;Execution</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="ch02.html" title="Chapter&nbsp;2.&nbsp;Pages"><link rel="prev" href="ch02.html" title="Chapter&nbsp;2.&nbsp;Pages"><link rel="next" href="ch02s03.html" title="2.3.&nbsp;Request Parameter Auto Binding"></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">2.2.&nbsp;Execution</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch02.html">Prev</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;2.&nbsp;Pages</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="ch02s03.html">Next</a></td></tr></table><hr></div><div class="sect1" title="2.2.&nbsp;Execution"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="execution"></a>2.2.&nbsp;Execution</h2></div></div></div><p>The Page class provide a number of empty handler methods which
subclasses can override to provide functionality:
</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/Page.html#onSecurityCheck()" target="_blank">onSecurityCheck()</a>
</p></li><li class="listitem"><p>
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/Page.html#onInit()" target="_blank">onInit()</a>
</p></li><li class="listitem"><p>
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/Page.html#onGet()" target="_blank">onGet()</a>
</p></li><li class="listitem"><p>
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/Page.html#onPost()" target="_blank">onPost()</a>
</p></li><li class="listitem"><p>
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/Page.html#onRender()" target="_blank">onRender()</a>
</p></li><li class="listitem"><p>
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/Page.html#onDestroy()" target="_blank">onDestroy()</a>
</p></li></ul></div><p>The ClickServlet relies on instantiating Pages using a public no
arguments constructor, so when you create Page subclasses you must ensure
you don't add an incompatible constructor. The GET request execution sequence
for Pages is summarized below in the Figure 2.
</p><div class="figure"><a name="get-sequence-diagram"></a><div class="figure-contents"><span class="inlinemediaobject"><img src="images/pages/get-sequence-diagram.png" alt="GET Request Sequence Diagram"></span></div><p xmlns:fo="http://www.w3.org/1999/XSL/Format" class="title"><i>Figure&nbsp;2.2.&nbsp;GET Request Sequence Diagram
</i></p></div><br class="figure-break"><p>Stepping through this GET request sequence, a new Page instance is
created and the attributes for the Page are set (context, format, headers,
path). Next, request parameter values are bound to any matching public
Page fields.
</p><p>Then the <code class="methodname">onSecurityCheck()</code> handler is executed.
This method can be used to ensure the user is authorized to access the page,
and if necessary abort any further processing.
</p><p>The next method invoked is <code class="methodname">onInit()</code>, this is
where you place any post constructor initialization code.
<code class="methodname">onInit()</code> is the ideal place to create controls such
as Forms, Fields and Tables. As illustrated by the diagram, after a Page's
<code class="methodname">onInit()</code> is called, each Control, available at that
stage, will have their <code class="methodname">onInit()</code> method called.
</p><p>
The next step is the processing of the Page's
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/Page.html#controls" target="_blank">controls</a>.
The ClickServlet gets the list of Controls from the page and then iterates
through the list calling <code class="methodname">onProcess()</code>. If any of the
Control's <code class="methodname">onProcess()</code> methods return false,
processing of subsequent controls and the Page's <code class="methodname">onGet()</code>
method is aborted.
</p><p>If everything is executing normally the Page's
<code class="methodname">onGet()</code> method is now called.
</p><p>The next step is rendering the page template to generate the displayed
HTML. The ClickServlet gets the model (<code class="classname">Map</code>) from the
Page then adds the following objects to the model:
</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>any public Page fields using the fields name</p></li><li class="listitem"><p>context &nbsp;-&nbsp; the Servlet context path, e.g. /mycorp</p></li><li class="listitem"><p>format &nbsp;-&nbsp; the
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/util/Format.html" target="_blank">Format</a>
object for formatting the display of objects.
</p></li><li class="listitem"><p>headElements &nbsp;-&nbsp; the HEAD <a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/element/Element.html" target="_blank">elements</a>,
excluding JavaScript, to include in the page header. Please see
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/util/PageImports.html" target="_blank">PageImports</a>
for more details.
</p></li><li class="listitem"><p>jsElements &nbsp;-&nbsp; the JavaScript imports and script blocks to
include in the pages footer. Please see
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/util/PageImports.html" target="_blank">PageImports</a>
for more details.
</p></li><li class="listitem"><p>messages &nbsp;-&nbsp; the
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/util/MessagesMap.html" target="_blank">MessagesMap</a>
adaptor for the Page
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/Page.html#getMessage(java.lang.String)" target="_blank">getMessage()</a>
method
</p></li><li class="listitem"><p>path &nbsp;-&nbsp; the
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/Page.html#path" target="_blank">path</a> of
the page template to render
</p></li><li class="listitem"><p>request &nbsp;-&nbsp; the pages
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/http/HttpServletRequest.html" target="_blank">HttpServletRequest</a>
object
</p></li><li class="listitem"><p>response &nbsp;-&nbsp; the pages
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/http/HttpServletResponse.html" target="_blank">HttpServletResponse</a>
object
</p></li><li class="listitem"><p>session &nbsp;-&nbsp; the
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/util/SessionMap.html" target="_blank">SessionMap</a>
adaptor for the users
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/http/HttpSession.html" target="_blank">HttpSession</a>
</p></li></ul></div><p>It then merges the template with the page model and writes out results
to the HttpServletResponse. When the model is being merged with the template,
any Controls in the model may be rendered using their
<code class="methodname">toString()</code> method.
</p><p>The final step in this sequence is invoking each control's
<code class="methodname">onDestroy()</code> method and lastly invoke the Page's
<code class="methodname">onDestroy()</code> method. This method can be used to
clean up resource associated with the Control or Page before it is garbage
collected. The <code class="methodname">onDestroy()</code> method is guaranteed to
be called even if an exception occurs in the previous steps.
</p><p>The execution sequence for POST requests is almost identical, except
the <code class="methodname">onPost()</code> method is invoked instead on
<code class="methodname">onGet()</code>. See the
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../../images/post-sequence-diagram.png" target="_blank">POST Request Sequence Diagram</a>.
</p><p>Another view on the execution flow of Pages is illustrated in the
Activity diagram below.
</p><div class="figure"><a name="activity-diagram"></a><div class="figure-contents"><span class="inlinemediaobject"><img src="images/pages/activity-diagram-small.png" alt="Page Execution Activity Diagram"></span></div><p xmlns:fo="http://www.w3.org/1999/XSL/Format" class="title"><i>Figure&nbsp;2.3.&nbsp;Page Execution Activity Diagram
</i></p></div><br class="figure-break"></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch02.html">Prev</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="ch02.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="ch02s03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&nbsp;2.&nbsp;Pages&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;2.3.&nbsp;Request Parameter Auto Binding</td></tr></table></div></body></html>