...
However, you often want to store some data on a single page, and have access to it in later requests to that same page, without having to store it in a database between requests. (To store values across multiple pages, see Persistent Page Data Session Storage.)
Making page data persist across requests to a single page is accomplished with the @Persist annotation. This annotation is applied to private instance fields of components:
Code Block |
---|
|
@Persist
private int value;
|
...
A suitably long session attribute name is used; it incorporates the name of the page, the nested component id, and the name of the field.
Code Block |
---|
language | java |
---|
title | Example: Session Strategy |
---|
|
@Persist
private int value;
|
...
The flash is typically used to store temporary messages that should only be displayed to the user once.
Code Block |
---|
language | java |
---|
title | Example: Flash Strategy |
---|
|
@Persist(PersistenceConstants.FLASH)
private int value;
|
...
Use client persistence with care, and store a minimal amount of data. Try to store the identity (that is, primary key) of an object, rather than the object itself.
Code Block |
---|
language | java |
---|
title | Example: Client Strategy |
---|
|
@Persist(PersistenceConstants.CLIENT)
private int value;
|
...
In Entity persistence, the field should store a Hibernate entity instance.
Code Block |
---|
language | java |
---|
title | "Hibernate Entity Strategy" |
---|
|
@Persist(HibernatePersistenceConstants.ENTITY)
private User user; |
...
The tapestry-jpa module uses a similar strategy. However, at the current time it can only store a persisted entity (one that has been saved to the database and has a primary key).
Code Block |
---|
language | java |
---|
title | "Example: JPA Entity Strategy" |
---|
|
@Persist(JpaPersistenceConstants.ENTITY)
private Account account; |
Persistence Strategy Inheritance
By default the value for the Persist annotation is the empty string. When this is true, then the actual strategy to be used is determined by a search up the component hierarchy.
For each component, the meta-data property tapestry.persistence-strategy
is checked. This can be specified using the @Meta annotation.
If the value is non-blank, then that strategy is used. This allows a component to control the persistence strategy used inside any sub-components (that don't explicitly use a different strategy).
...
Include Page |
---|
| Clustering Issues |
---|
| Clustering Issues |
---|
|
Code Block |
---|
language | java |
---|
title | Example: Entity Session Strategy |
---|
|
@Persist(HibernatePersistenceConstants.ENTITY)
private User user; |
Code Block |
---|
language | java |
---|
title | "Example:JAP Session Strategy" |
---|
|
@Persist(JpaPersistenceConstants.ENTITY)
private Account account; |