tapestry study ----04

来源:互联网 发布:淘宝金酷娃玩具车视频 编辑:程序博客网 时间:2024/06/10 07:44

To create a link to show a page, use a PageLink component. To create a link to show a page that takes parameters,
either use a DirectLink to pass the parameters to the listener which will activate the page using the bucket brigade
pattern.
If a page takes parameters, it is dangerously easy to come to believe that the parameters are still set when you click a
link or submit the form on that page. This is NOT the case because a new page may be taken from the pool. To
maintain the parameters, either put them into a DirectLink or Hidden fields or store them as the client persistent
properties. If you use client persistent properties, when the page is rendered, the properties will be stored as query
parameters in links and hidden fields in forms. When the user clicks a link or submits a form, if during the handling of
the HTTP request that page is loaded, the properties will be retrieved from the HTTP query and store into the page
object.
You may also store a persistent property into the session. However, you must make sure this data is per-user instead
of per-page, otherwise the "Back" button may cause surprises.
A session is a Map of key-value pairs stored in the memory of the server for each user. The server puts the session id
into a cookie in the browser so that it can lookup the session on the subsequent requests. Or it could can use URL
rewriting to pass the session id along.
Every Tapestry application has a list of application state objects. Each row in the list specifies the name of the object,
its Java class and its scope. The scope can be "session" or "application" and it will determine this object will be put into
the session or into a global area. To add a row to that list, edit the Hivemind module descriptor (at the resource path /
META-INF/hivemodule.xml) to add a contribution to the configuration named tapestry.state.ApplicationObjects. To
access the object, use @InjectState or <inject type="state">. The object will be created automatically if it doesn't exist
yet. Whenever you modify the hivemodule.xml file, don't forget to reload the application because it is only read at start
up. If you're putting an object into the session, your Java class should implement Serializable because the server may
save the data in the session to disk. To check if an application state object exists, use @InjectStateFlag or <inject
type="state-flag">.
If your Form contains multiple Submit components, you need to distinguish which one was clicked. You can have a
different listener for each of themSubmit component. The listener of the Submit component that is clicked will be called
just before the Form calls its own listener (if any). Another solution is to let the Submit component set its "selected"
parameter to its "tag" value and then in the Form's listener check which one was clicked.
To let a page protect itself, let it implement PageValidateListener and implement the pageValidate() method. It will be
called when the page is activated. If something is wrong, it can redirect to another page by throwing a
PageRedirectException. This is most useful when the page requires that the user has logged in.
Commonly a login page needs to remember the next page to show after the login. The next page can be represented
using an ICallback object. A PageCallback stores just the page name. An ExternalPageCallback stores the page name
and an array of parameters. That page must implement IExternalPage for this to work. The added benefit is that this
page can now be invoked with parameters using a URL.
To implement logout, use the Restart service provided by Tapestry so that the session is deleted. To call it, use a
ServiceLink.
<page-specification>
...
<component id="logoutLink" type="ServiceLink">
<binding name="service" value="literal:restart"/>
</component>
</page-specification>
The name of the service to call. The default
prefix is ognl, so you need to tell it this is a
literal here. Or you could quote it with single
quotes.
<page-specification>
...
<component id="logoutLink" type="ServiceLink">
<binding name="service" value="'restart'"/>
</component>
</page-specification>
138 Chapter 4 Creating an e-Shop
If you need to use a component at two difference locations in the same page with the same parameter bindings, you
should use the copy-of feature.
Instead of setting the Java class in each .page file, you can list some Java packages in the application specification and
Tapestry will look for a class with the name of the page in those packages.