Apache Tapestry Storage - Apache Tapestry

What is the use of Apache Tapestry Storage?

Every web application will have a storage for storing certain user data like user object, user preferences, etc. For example, in a shopping cart application, products selected by users should be saved in a cart until the user prefers to buy the products. These items can be saved in a database, but it will be too expensive as all the users will not buy the selected items. So temporary arrangement should be made to store the items. Apache Tapestry provides 2 ways to persist the data and they are

  • Persistence page data
  • Session Storage

What is Persistence Page Data?

Persistence Page Data is a simple concept to store data in a single page between requests and it is called as Page Level Persistence. It can be done using the @Persist annotation.

Once a field is annotated using @Persist, field's value will be persisted across request and if the value is changed during request, then it will be reflected when it is accessed next time. Apache Tapestry has 5 types of strategies for implementing the @Persist concept:

  • Session Strategy −data is persisted using the Session and it is a default strategy.
  • Flash Strategy − data is persisted using Session as well, but it is a very short lived one. Data will be available in only one subsequent request.

  • Client Strategy − Data is persisted in the client side such as URL query string, hidden field in the form, etc.

  • Hibernate Entity Strategy − data is persisted using the Hibernate module as Entity. Entity will be stored in Hibernate and its reference (Java class name and its primary key) will be saved as token in HttpSession. Entity will be restored by using the token available in HttpSession.

  • JPA Entity Strategy − data is persisted using a JPA module and will store only Entity.

Session Storage

Session storage is an advanced concept of storing data which should be available across pages like data in multiple page wizard, logged in user details, etc. Session Storage provides 2 options, one to store complex object and another to store simple values

  • Session Store Object – is used for storing complex object.
  • Session Attributes – is used for storing simple values.

How Session Store Object (SSO) is created?

SSO can be created using @SessionStore annotation and stores the object using type of the object. For example, Cart Object will be stored using a Cart class name as token. So, any complex object can be stored once in an application.

SSO is a specialized store and is used for storing complex / special object. Simple data types can also be stored using an SSO, but storing simple data types like String makes it only store one “String” value in the application. Using a single “String” value in the application is not possible. For this, users can use simple data types as Apache Tapestry provides Session Attributes.

What are Session Attributes?

Session Attributes enable the data to be stored by name instead of its type.

Session Attributes by default use field names to refer the data in session. Reference name can be changed by annotation parameter as shown below

One of the main reasons behind using name as session reference is that becuse accidentally same name can be used in more than one class / page. In this case, data stored maybe changed unexpectedly. To handle this, it is suggested to use name along with class / page name and package name like com.myapp.pages.register.email, where com.myapp.pages is the package name, register is the page / class name and finally email is variable (to be stored) name.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Apache Tapestry Topics