Foren

Separate variables beetween portlet instantion

Vad Pyl, geändert vor 8 Jahren.

Separate variables beetween portlet instantion

New Member Beiträge: 15 Beitrittsdatum: 27.03.15 Neueste Beiträge
Hello,
I am using Liferay 6.2.

Generaly I have one portlet. I added my portlet on two portal pages, thus we have portlet 1 and portlet 2. (One portelet but on two different portal pages.)
For example 2 users are on portal at the same time. User A is on portlet 1 and User B is on portlet 2.
The problem is, when user A set some variables of potlet 1, portlet 2 on another page also will have this variable set same. I use private static type variables and they are visible for all instance of portlet.
How to make variables is visible only on portlet(portal page instance), and be a possibility use portlets parallely(same variable of portlet have different values)
I have instanceable false portlet, and portlet can be on portal page only once.

Hope I clear explained. Thanx.
thumbnail
Alexey Kakunin, geändert vor 8 Jahren.

RE: Separate variables beetween portlet instantion

Liferay Master Beiträge: 621 Beitrittsdatum: 07.07.08 Neueste Beiträge
In your case best thing is to store this variable into portlet session.

To set some value:


		PortletSession portletSession = request.getPortletSession();
		portletSession.setAttribute("attribute name", "attribute value");


Later you can get this value by using:

		portletSession.getAttribute("attribute name");


Let me know if it helped.

====
Alexey Kakunin
Liferay Experts in Russia
thumbnail
David H Nebinger, geändert vor 8 Jahren.

RE: Separate variables beetween portlet instantion (Antwort)

Liferay Legend Beiträge: 14916 Beitrittsdatum: 02.09.06 Neueste Beiträge
Vadim Pyl:
when user A set some variables of potlet 1, portlet 2 on another page also will have this variable set same. I use private static type variables and they are visible for all instance of portlet.


Um, by the language specification that's how static member fields work. Static means there is a single value shared by all instances of the class. (well, within the context of the class loader, etc., but you should get the gist).

Use a session variable if the value is unique to the user, a portlet pref if it is unique to the portlet instance, or keep using static if it is shared by all.
john lu, geändert vor 8 Jahren.

RE: Separate variables beetween portlet instantion

New Member Beiträge: 15 Beitrittsdatum: 27.03.15 Neueste Beiträge
Thank you for answers. I became closer to solution. But also have some question.
PortletSession is good thing. Variables are unique even on same portlet instance in different session.
I try to found method which will store variables in portlet instance for all user(but not for users of another instances of portlet).
e.g I need to save hashmap of users votes. All of users use one intance of portlet at the same time. Parallely another group of users can use another instance of portlet.
Is it good way to store votes in preferences of portlet instance? Is it possible to parse hasmap with votes to string and keep it in preferences?
Or exist another way to store variables in portlet instance?
thumbnail
Alexey Kakunin, geändert vor 8 Jahren.

RE: Separate variables beetween portlet instantion (Antwort)

Liferay Master Beiträge: 621 Beitrittsdatum: 07.07.08 Neueste Beiträge
Hi!

If you have some complex data to store - it is better to implement own table in database and store there (for example via using Service Builder). But it is required much more development. Portlet Preferences more designed for preferences - not for storing portlet-specific data.

Anyway, as simple and quick solution you still can use portlet preference. And yes - you can serialize HashMap into string and store it in preferences, or, since Portlet Preferences is Map by itself (it is stored values in key-value way) you can directly map your Map with votes results into Map in preferences.

====
Alexey Kakunin
Liferay Experts in Russia
john lu, geändert vor 8 Jahren.

RE: Separate variables beetween portlet instantion

New Member Beiträge: 15 Beitrittsdatum: 27.03.15 Neueste Beiträge
Thank you very much. I'll try to do it using preferences.
thumbnail
Sravan Kumar Chalvadi, geändert vor 7 Jahren.

RE: Separate variables beetween portlet instantion

New Member Beiträge: 22 Beitrittsdatum: 13.01.16 Neueste Beiträge
john lu:
Thank you very much. I'll try to do it using preferences.


Hi!

I have a portlet with private variable. Multiple users accessing class have same reference to the variable, as in changes to the variable by a user affecting other user. Don't we have portlet/controller instance created per user?

Fyi, instanceable set to false and it is in liferay 7.
thumbnail
David H Nebinger, geändert vor 7 Jahren.

RE: Separate variables beetween portlet instantion

Liferay Legend Beiträge: 14916 Beitrittsdatum: 02.09.06 Neueste Beiträge
Sravan Kumar Chalvadi:
I have a portlet with private variable. Multiple users accessing class have same reference to the variable, as in changes to the variable by a user affecting other user. Don't we have portlet/controller instance created per user?


No. There is a single portlet class used to service all requests. It is not single instance per user. Most servers could never handle a site that contained, say, 100 portlets that were all separate instance per user, the memory and processing power alone would bring even huge clusters to their knees.

Fyi, instanceable set to false and it is in liferay 7.


That has nothing to do with anything. Instanceable just determines how IDs are handled and whether dups are allowed on the page.

For example, only one login portlet is allowed. If you look at the form field ID, you'll find something like _58_login for the email address. The field is namespaced for the portlet, but that's it.

Then look at the web content display portlet. It's fields will have not only the portlet ID in front, but it will also have a random hex string at the end.

In each case the goal is to generate a unique ID on the page - non-instanceable means portlet ID + field ID will work, but for instanceable you need an additional random string to guarantee unique IDs.

All of that said, in the portal runtime there is a single login portlet instance handling all requests, and there is also a single web content display portlet handling all requests.

In no way can you assume that you have any access to some local field value in any class that is unique to a current user session.










Come meet me at the 2017 LSNA!