Java Reference
In-Depth Information
Tim Says. . .
ActionBeanContext Isn't Limited to HttpSession
Freddy covered an example of using the action bean context
to insulate the rest of your code from accessing things that are
stored in
HttpSession
. In my opinion, an even greater benefit is
that because your code is insulated from the implementation
details, you can actually change how you store and access
items without any impact.
Let's say, for example, that your
HttpSession
object is getting too
big—you're storing too much in it. In such a situation you might
choose another strategy: store some identifier in a cookie and
then look up the actual object when needed. The following is
an example of how you might do that, and no caller will ever
know the difference:
package
stripesbook.ext;
public class
MyActionBeanContext
extends
ActionBeanContext {
private static final
String FOLDER = "folder";
private
Folder currentFolder =
null
;
public void
setCurrentFolder(Folder folder) {
Cookie c =
new
Cookie(FOLDER, String.valueOf(folder.getId()))
getResponse().addCookie(c);
}
public
Folder getCurrentFolder() {
if
(
this
.currentFolder ==
null
) {
Cookie c = findCookie(FOLDER);
int
id = (c !=
null
) Integer.parseInt(c.getValue()) : 0;
this
.currentFolder = MockFolderDao.getInstance().read(id);
}
return this
.currentFolder;
}
private
Cookie findCookie(String name) {
for
(Cookie c : getRequest().getCookies()) {
if
(c.getName().equals(name)) {
return
c;
}
}
return null
;
}
}
Search WWH ::
Custom Search