Java Reference
In-Depth Information
an arbitrary key, so we'll have to declare constants somewhere in our
code. The session returns the data as an Object , so we have to cast that
back to our original data type. For example:
// Current folder
Folder folder = ...;
// Key used to store the folder
String FOLDER = "folder";
// Store the folder in the session
HttpSession session = request.getSession();
session.setAttribute(FOLDER, folder);
// Retrieve the folder from the session
// Needs a cast because getAttribute() returns Object
folder = (Folder) session.getAttribute(FOLDER);
Ugh. That's not pretty. It's nevertheless necessary if we're going to use
the session. But we can hide this code in the back of the closet and
rarely have to look at it again. Stripes provides a clean solution for
session-related code with the ActionBeanContext class. As you know,
this object is always available in action beans via the getContext ( )
method. Also, ActionBeanContext provides getRequest ( ) to obtain the cur-
rent request and, from there, the session. All this makes ActionBeanCon-
text the perfect place to encapsulate the code that deals with HttpSession
and to shield the rest of the application from constants, casts, and ugly
goblins.
By creating a class that extends ActionBeanContext , we can add meth-
ods that manage objects in the session. A custom ActionBeanContext
subclass is considered an extension, so Stripes loads it automatically
if we put the class in a package that we configured with the Exten-
sion.Packages parameter in web.xml . 1
The following MyActionBeanContext class stores and retrieves the cur-
rent Folder object using the session, taking care of setting a default
when no previous selection exists.
See the sidebar on page 115 if you need a refresher on Stripes extensions.
1.
 
 
Search WWH ::




Custom Search