In JAFFA, we typically use 'request' scope for FormBeans, that means they hold no state between requests.
The reason for this as mention in the 'FormKey' section is that we allow multiple instances of the same form, so
storing the form in the HttpSession with just the formname as a key is not a viable option.
So, as we re-create the FormBean, we don't want to have to rebuild the widget models, which in the case of things like
a UserGrid many contain thousands of inner widgets. So, to avoid this we provide a component instance based
widget cache
which stores the widgets models for the form bean in the UserSession object.
The key features of the widget cache are ...
- Stored in UserSession
- There is one cache per component instance (componentId)
- Within the cache each widget needs a unique name
- The FormBase exposes a convenient interface for adding and reading component from the cache
This is a typical example of a widget using the cache. In this case if the widget does not exits, it
looks for it in the cache. If its not cached, it gets constructed and stored in the cache for next time.
Taken from SessionExplorerForm.java |
public GridModel getSessionsWM() {
if (w_sessions == null) {
w_sessions = (GridModel) getWidgetCache().getModel("sessions");
if (w_sessions == null) {
w_sessions = createSessionsModel();
getWidgetCache().addModel("sessions", w_sessions);
}
}
return w_sessions;
}
|
Note: The name used to store the object in the cache is unique to that component instance. What this means is that
if a given component has multiple JSP's (and FormBean) associated with it and you want to share widget models across
pages, you can by using the same name. HOWEVER, if you accidentally use the same field name on two forms in the same
component and use the same name to cache them, you will get huge problems with the most recent post overwriting that
of the other form. If they are the same name and different models, chances are you start getting ClassCastExceptions!
|