I’ve been busy working away on a new XPages
application in work that implements a typical shopping cart like you might
find on Amazon or other online retail websites. When I started planning
the application I hit upon the idea of storing the items that the user
had added to the shopping cart into a variable in the sessionScope. The
sessionScope, in xPages, is an area of memory that you can store values
for use by server side javascript in your application. Each user who logs
into the application has their own sessionScope as opposed to the applicationScope
that is shared across all users of the xPages application. My idea was
that by using the session scope I wouldn’t have to create any temporary
backend documents to store the selected items.
Normally you put stuff into the sessionScope
using the ‘put’ method, for example sessionScope.put("userFirstName","Declan").
You can also write this in shorthand as sessionScope.userFirstName = "Declan".
To get stuff out of the sessionScope you use the get method, for example
sessionScope.get("userFirstName") or in shorthand sessionScope.userFirstName
In javascript you can store anything
inside a variable including another object. Based on the ‘Namespacing
Scoped Variables‘ blog entry by Tim Tripcony I figured that
I’d be able to use my sessionScope to store an array of Unique ID’s and
their quantities. After talking with Tim about the idea he suggested that
I put a hashmap object into my variable in the sessionScope.
To initialize the variable I’m using
the following line of code.
sessionScope.cartItems
= (sessionScope.cartItems || new java.util.HashMap());
This sets the sessionScope.cartItems
to be equal to itself so that I don’t accidentally clear it’s value, however
if sessionScope.cartItems does not exist yet then the second half of the
statement is carried out which created the HashMap object in the sessionScope.
Now that I have a hashmap inside the
sessionScope I can use all the methods on it as I would the sessionScope
var thisDocID
= currentDocument.getDocument().getUniversalID();
var thisQuantity = getComponent("comboBox1").getValue();
sessionScope.cartItems.put(thisDocID,thisQuantity)
By using the UNID as the key I can easily
relate the item in the cart back to the item documents in my notes database.
I can even use a repeat control bound to sessionScope.cartItems.keySet();
loop through all the items in the shopping cart. I could use sessionScope.cartItems.size();
to find out how many items are in the shopping cart and even sessionScope.cartItems.clear();
to empty the cart out.
Hopefully knowing that you can store
different types of objects in the sessionScope will give you some ideas
for your own applications.
Awesome find. This greatly expands the power of the scope variables! I thought I had tried storing an object in a session scope and it didn’t work. But I’m learning everyday with xPages that what I knew last week is not what I will know next week. I wonder what is the negative to overusing scoped variables? At what point will we start to see memory problems. I’m really starting to use these scoped variables quite a bit. And now with this added functionality – I could easily get carried away.
LikeLike
@1 – Because of the Java garbage collection rules, you are not allowed to store Domino objects in Session or Application scoped variables. Only in View and Request scope.As far as memory problems go, I think you’ll find it pretty challenging to create memory problems at the session scope. Unless you’re storing binary file data (such as images) at the session level, you’re unlikely to consume a serious amount of memory with various String objects that would typically be the subjects of this type of scope caching.By the way, Declan, the sessionScope *IS* a HashMap. So you’re really storing other maps in a map. Which is the great thing about HashMaps, because you can put any crazy structures you want into them, and they nest beautifully.
LikeLike
LikeLike
i don’t undertand what the sorprendent in this!!!Ce
LikeLike
Does it work? I tried the same, but I get an error, using a Domino 8.5.2 Fixpack 1 Server:
Here is my code:
sessionScope.logSettings = (sessionScope.logSettings || new java.util.HashMap());
var logDBPath = …(get the value from a document)
sessionScope.logSettings.put(logDBPath, logDBPath);
The error I get is: com.ibm.jscript.InterpretException: Script interpreter error, line=45, col=42: Java method ‘put(undefined, undefined)’ on java class ‘java.util.HashMap’ not found
Any help is much appreciated! Thanks very much!
LikeLike
Does it work? I tried the same, but I get an error, using a Domino 8.5.2 Fixpack 1 Server:
Here is my code:
sessionScope.logSettings = (sessionScope.logSettings || new java.util.HashMap());
var logDBPath = …(get the value from a document)
sessionScope.logSettings.put(logDBPath, logDBPath);
The error I get is: com.ibm.jscript.InterpretException: Script interpreter error, line=45, col=42: Java method ‘put(undefined, undefined)’ on java class ‘java.util.HashMap’ not found
Any help is much appreciated! Thanks very much!
LikeLike