Note |
---|
This is an advanced topic. Most users won't ever need to know anything about the page life cycle. |
...
In Tapestry, you are free to develop your presentation objects, page and components classes, as ordinary objects, complete with instance variables and so forth.
This is somewhat revolutionary in terms of web development in Java. Using By comparison, using traditional servlets, or Struts, your presentation objects (Servlets, or Struts Actions, or the equivalent in other frameworks) are stateless singletons. That is, a single instance is created, and all incoming requests are threaded through that single instance. Because multiple requests are handled by many different threads, this means that the single instancesingleton's variable instance variables are useless ... any value written into an instance variable would immediately be overwritten by a different thread. Thus, it is necessary to use the Servlet API's HttpServletRequest object to store per-request data, and the HttpSession object to store data between requests.
Div | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
|
Tapestry takes a very different approach.
In Tapestry, you will have many different instances of any particular page, each either in use for a single request (on a single thread), or waiting in a page pool to be used.each page is a singleton, but with a per thread map of field names & values that Tapestry invisibly manages for you.
With this approachBy reserving page instances to particular threads, all the difficult, ugly issues related to multi-threading go by the wayside. Instead, familiar, simple coding practices (using ordinary methods and fields) can be used.
However, there's a risk: it would be a disaster if data could "bleed" from one request to another. Imagine the outcome in a banking application if the first user's account number and password became the default for the second user to reach the application!
Tapestry takes special care to purge all instance variables back to their default value at the end of each request.
The end result is that all pages in the pool are entirely equivalent to each other; it doesn't matter which instance is used for processing any particular request.
Remember that the page instance is just the tip of the iceberg: a page instance encompasses the page component, its templates, all of its parameter bindings, tokens read from its template and (recursively) the same thing for all components inside the page. It adds up.
Info |
---|
Tapestry 5.0 and 5.1 used page pooling, rather than a singleton page with a per-thread map, to achieve the same effect. |
The page life cycle is quite simple:
- When first needed, a page is loaded. Loading a page involves instantiating the components of the page and connecting them together.
- Once a page is loaded, it is attached to the current request. Remember that there will be many threads, each handling its own request to the same page.
- At the end of a request, after a response has been sent to the client, the page is detached from the request. This is a chance to perform any cleanup needed for the page.
Page Life Cycle Methods
There are rare occasions where it is useful for a component to perform some operations, usually some kind of initialization or caching, based on the life cycle of the page.
As with component rendering, you have the ability to make your components "aware" of these events by telling Tapestry what methods to invoke for each.
Page life cycle methods should take no parameters and return void.
You have the choice of attaching an annotation to a method, or simply using the method naming conventions:
Annotation | Method Name | When Called |
---|---|---|
pageLoaded() | After the page is fully loaded | |
pageAttached() | After the page is attached to the request. | |
@PageReset | pageReset() | After the page is activated, except when requesting the same page |
pageDetached() | AFter the page is detached from the request. |
The @PageReset life cycle (only for Tapestry 5.2 and later) is invoked on a page render request when the page is linked to from some other page of the application (but not on a link to the same page), or upon a reload of the page in the browser. This is to allow the page to reset its state, if any, when a user returns to the page from some other part of the applicationA page instance will be "checked out" of the pool for a short period of time: a few milliseconds to service a typical request. Because of this, it is generally the case that Tapestry can handle a large number of end users with a relatively small pool of page instances.
Comparison to JavaServer Pages
JSPs also use a caching mechanism; the JSP itself is compiled into a Java servlet class, and acts as a singleton.act as singletons. However, the individual JSP tags are pooled.
...
The operations Tapestry does once per request are instead executed dozens or potentially hundreds of times (dependending depending the complexity of the page, and if any nested loops occur).
...
Tapestry can also take advantage of its more coarse grained caching to optimize how data moves, via parameters, between components. This means that Tapestry pages will actually speed up after they render the first time.
Page Pool Configuration
Note |
---|
This section is related to versions of Tapestry prior to 5.2. Modern Tapestry uses an alternate approach that allows a single page instance to be shared across many request processing threads. |
In Tapestry 5.0 and 5.1, a Tapestry's page pool is used to store page instances. The pool is "keyed" on the name of the page (such as "start") and the locale for the page (such as "en" or "fr").
...
When a page is first accessed in a request, it is taken from the pool. Tapestry has some configuration values that control the details of how and when page instances are created.
...
If performance is absolute and you have lots of memory, then increase the soft and hard limit and reduce the soft wait. This encourages Tapestry to create more page instances and not wait as long to re-use existing instances.
Page Life Cycle Methods
There are a few situations where it is useful for a component to perform some operations, usually some kind of initialization or caching, based on the life cycle of the page.
The page life cycle is quite simple. When first needed, a page is loaded. Loading a page involves instantiating the components of the page and connecting them together.
Once a page is loaded, it is attached to the current request. Remember that there will be many threads, each handling its own request. In many cases, there will be multiple copies of the same page attached to different requests (and different threads). This is how Tapestry keeps you from worrying about multi-threading issues ... the objects involved in any request are reserved to just that request (and just that thread).
At the end of a request, after a response has been sent to the client, the page is detached from the request. This is a chance to perform a lot of cleanup of the page, discarding temporary objects (so that they can be reclaimed by the garbage collector) and otherwise returning the page to its pristine state. After detaching, a page is placed into the page pool, where it will await reuse for some future request (likely by a completely different user).
As with component rendering, you have the ability to make your components "aware" of these events by identifying methods to be invoked.
You have the choice of attaching an annotation to a method, or simply naming the method correctly.
Page life cycle methods should take no parameters and return void.
The annotations / method names are:
...
Scrollbar |
---|