blob: 4ef0e25c77d0beb71807c3cf93509ac4bd98d0c1 [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>2.9.&nbsp;Stateful Pages</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="ch02s08.html" title="2.8.&nbsp;Direct Rendering"><link rel="next" href="ch02s10.html" title="2.10.&nbsp;Error Handling"></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.9.&nbsp;Stateful Pages</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch02s08.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="ch02s10.html">Next</a></td></tr></table><hr></div><div class="sect1" title="2.9.&nbsp;Stateful Pages"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="stateful-pages"></a>2.9.&nbsp;Stateful Pages</h2></div></div></div><p><span class="symbol">PLEASE NOTE:</span> stateful pages have been deprecated in
Click 2.3.0 and will be removed in a future release. Do not use stateful pages
in your applications. Instead use stateful controls or HttpSession to store
state between requests.
</p><p>Click supports stateful pages where the state of the page is saved
between the users requests. Stateful pages are useful in a number of
scenarios including:
</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>Search page and edit page interactions. In this scenario you
navigage from a stateful search page which may have filter criteria
applied to an object edit page. Once object update has been completed
on the edit page, the user is redirected to the search page and the
stateful filter criteria still applies.
</p></li><li class="listitem"><p>Complex pages with multiple forms and or tables which need to
maintain their state between interactions.
</p></li></ul></div><p>To make a page stateful you simply need to set the page
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/Page.html#stateful" target="_blank">stateful</a>
property to true, have the page implement the <code class="classname">Serializable</code>
interface and set the <code class="literal">serialVersionUID</code> indicator.
For example:
</p><pre class="programlisting"><span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">package</span> com.mycorp.page;
<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">import</span> java.io.Serializable;
<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">import</span> org.apache.click.Page;
<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> SearchPage <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">implements</span> Serializable {
<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">private</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">static</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">final</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">long</span> serialVersionUID = <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="lit">1L</span>;
<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">public</span> SearchPage() {
setStateful(true);
..
}
}</pre><p>Stateful page instances are stored in the user's
<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>
using the pages class name as the key. In the example above the page would
be stored in the users session using the class name:
<code class="classname">com.mycorp.page.SearchPage</code>
</p><div class="sect2" title="2.9.1.&nbsp;Page Creation"><div class="titlepage"><div><div><h3 class="title"><a name="page-creation"></a>2.9.1.&nbsp;Page Creation</h3></div></div></div><p>With stateful pages they are only created once, after which they
are retrieved from the session. However page event handlers are invoked
for each request, including the <code class="methodname">onInit()</code> method.
</p><p>When you are creating stateful pages you typically place all your
control creation code in the Pages constructor so it is invoked only once.
It is important not to place control creation code in the
<code class="methodname">onInit()</code> method which will be invoked with each
request.
</p><p>If you have dynamic control creation code you would typically place
this in the <code class="methodname">onInit()</code> method, but you will need to
take care that controls and or models are not already present in the page.
</p></div><div class="sect2" title="2.9.2.&nbsp;Page Execution"><div class="titlepage"><div><div><h3 class="title"><a name="page-execution"></a>2.9.2.&nbsp;Page Execution</h3></div></div></div><p>The default Click page execution model is thread safe as a new
Page instance is created for each request and thread. With stateful
pages a user will have a single page instance which is reused in multiple
requests and threads. To ensure page execution is thread safe, users page
instances are synchronized so only one request thread can execute a page
instance at any one time.
</p></div><div class="sect2" title="2.9.3.&nbsp;Page Destruction"><div class="titlepage"><div><div><h3 class="title"><a name="page-destruction"></a>2.9.3.&nbsp;Page Destruction</h3></div></div></div><p>After normal page instances have been executed, they are
de-referenced and garbage collected by the JVM. However with stateful
pages they are stored in the users <code class="classname">HttpSession</code> so
care needs to be take not to store too many objects in stateful page
instances which may cause memory and performance issues.
</p><p>When pages have completed their execution, all the Page's controls
<code class="methodname">onDestroy()</code> methods are invoked, and then the
Page's <code class="methodname">onDestroy()</code> method is invoked. This is
your opportunity to de-reference any large sets or graphs. For example the
Table control by default de-references its rowList in its
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/control/Table.html#onDestroy()" target="_blank">onDestory()</a>
method.
</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch02s08.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="ch02s10.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">2.8.&nbsp;Direct Rendering&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.10.&nbsp;Error Handling</td></tr></table></div></body></html>