Categories
Learning XPages XPages

Viewer Question about scoped variables in XPages

So I got this in my inbox :

Dave,

Do you happen to know of a good article or video that talks about scope variables (i.e. applicationScope and sessionScope ) and passing values between different web pages?  Passing information like unid, user name and contact information (i.e. phone and address).

So loading the variables and writing the information to fields on different pages.

Thanks,

I don’t have an article to point to so I figure I’ll just give it my best shot and make one up.

First let’s remember that applicationScope is for the whole app.  Anything you put in there is available to all users. So you would not want to put in anything you listed in your email. The unid, user name, etc…  are all user specific.

Now, sessionScope is an interesting case, and just so we’re clear, sessionScope is often thought to be the USERS session BUT IT’S NOT. It’s really the BROWSERS session. For example, if you are using sessionScope for things and the user logs out and then a different user logs in from the same browser – assuming it’s not been closed…  it’s possible the second user might get some left over values from the first user. So you have to be careful and sometimes you need to manually clear sessionScope.

Often people new to XPages might use sessionScope to hold things like a unid, or maybe other things about the users navigation status like last page or whatever. These are NOT good things to put into sessionScope because if the user opens your web app in another tab and starts doing 2 things at once then putting data like unids and stuff in sessionScope will be a problem.  The users second tab will interfere with the first tab.

What sessionScope IS good for is things like a shopping cart or the user and contact information.  Something that’s for the user and needs to span multiple pages.  Things like UNID’s or keys, I always try to put in the URL itself.  This solves the problem of 2 browser tabs bumping into each other and also makes the pages easier to bookmark for later use if need be.

Personally I used to use sessionScope for EVERYTHING when I started out.  Now I use it much less.  I do much more with viewScope.  Conceptually what I do is in the beforePageLoad event, I’ll grab any parameters I need from the URL and anything I need from session or applicationScopes and then use that to continue loading my page.  In the example of a “company document”.  If I have a main page I’ll pass the unid or key (I use keys more then unids) in via the URL and grab that and load the data that I want.  Now if there’s a link to another page related to the same company I’ll just keep that unid/key in the url and simply reload the company from disc on the next page.  So it’s kind of a bummer to reload the company data for each page, but doing so makes things a lot easier.  If you have a big app there are caching solutions to aid this and we use some in the day job, but I’ve never really found it to be a problem.

So with the information you’ve given me, I’d suggest you keep your unid/key in the url.  UserName should be global really so I’m not sure if you really need to store that anywhere, but yes I’d store the contact information in scope.  If you know Java that could be a small managed bean or other object that lives in a pageController.  If you’re using SSJS you could make several variables – ssAddress1, ssCity, ssState, etc…  and put those in sessionScope.

But I’d probably sooner make a little map and put the map into sessionScope rather then having a ton of variables I need to track… I’ve done this on NotesIn9 several times I think…  something like

var myMap = new java.util.HashMap();
myMap.put(“address1”, ..myValue..);
myMap.put(“city”, ..myCityValue..);
sessionScope.put(“userAddress”, myMap);

then to use the map it should look something like this :

document.replaceItemValue(“address1”, sessionScope.userAddress.address1);

(Note I’m not typing in an editor and testing…  but that should work or be REALLY close.)

Creating and putting your own map into scope will at least let you group the data together.

 

Hopefully that helps a little bit.  If not just let me know.

Good Luck!

Dave