- Web site & download: http://sourceforge.net/projects/stripes-stuff/
- View source: http://stripes-stuff.svn.sourceforge.net/viewvc/stripes-stuff/
Sometimes it may be more simple to save specific fields in session. It may also take too much memory to save a complete action bean in session.
For this problem, I created a new interceptor which will scan action beans for @Session annotated fields. It will automatically store these fields in session after the event completes and will restore the fields automatically on subsequent requests. When validation error occurs in any phase, fields will not be saved in session as state would normally become inconsistent.
In addition if a request parameter would replace the field, the field will take the value from the request as expected.
Simply add org.stripesstuff.plugin.session to extensions packages in web.xml. A common configuration will look like this.
|maxTime|| Number of minutes before field is removed from session automatically.
|key|| Key to use to store field in session.
|actionBean.getClass() + "#" + field.getName()|
|If serializable is false and maxTime is negative, field will be removed upon session serialization.|
Simply use session.getAttribute(key).
Just calling session.removeAttribute(key) will not work in all cases. SessionStoreInterceptor saves fields in session after event completes and only if no validation error occurs. To remove object from session manually, the best solutions are:
- No validation error: set field to null in the event (if it's not a primitive).
- Validation errors: use session.removeAttribute(key).
The key used is the key defined in key attribute of @Session annotation.
When no key attribute is defined, the default key used to store fields is actionBean.getClass() + "#" + field.getName().
For instance, if action bean is ca.qc.ircm.web.MyActionBean and field is myField, the default key will be "ca.qc.ircm.web.MyActionBean#myField".
When a checkbox is checked, a parameter is sent to server. When checkbox is unchecked, nothing is sent to server.
This means that once a @Session annotated checkbox boolean field is checked and the event executes successfully, the boolean field will be true for as long as the field stays in session. Don't use @Session on checkboxes unless you can known that checkbox was unchecked.
Consider this action bean and jsp.
As you can see, the counter field of CounterAction class has a @Session annotation. This will keep track of the number of clicks made on "count" button. It is also possible to add a specific amount to the counter using countAmount.
- OverwriteParam. When set, the interceptor will overwrite any modifications made from submitted fields that could modify the field in BindingAndValidation lifecycle.
- SaveOnError. When set, the interceptor will store the value in session even when validation error occurs. Some specific lifecyle could also be specified to limit the lifecycle where stored fields could be overwritten.
- None to report for now.
Please report any bugs to me at Christian.Poitras@ircm.qc.ca