Forums de discussion

Update session variable using Ajax

Nate Shaw, modifié il y a 13 années.

Update session variable using Ajax

Junior Member Publications: 49 Date d'inscription: 26/10/10 Publications récentes
I am looking to try to update a session variable when a user clicks a button on a page (JavaScript Ajax call in a custom theme). Is there a way that I can accomplish this using a service or some other built-in Liferay functionality? Or, will I have to write my own Java code to accomplish this?

I understand the JavaScript side of things fine. What I really need to know is if there is something I can call in Liferay to update the session variable. If there is not, what do I need to do to write my own class to do this?

Below is the JavaScript code I have written. Note the onClose function where the Ajax call will need to take place.


<script language="JavaScript" type="text/javascript">
		AUI().use('liferay-notice', function(A){
			new Liferay.Notice({
				content: "Your password will expire in ${passwordExpiresDays} days! <a href='$resetPasswordUrl' target='_blank'>Reset it now</a> ",
				toggleText: false,
				//noticeClass: "$noticeClass",
				type: "$noticeType",
				closeText: "Dismiss",
				onClose: function(){
					//need to use Ajax to set "showPasswordExpirationNotice" session variable to false. How do I do this???
					alert("dismissing notice");
				}
			});
		});
	</script>


Thanks for the help!
thumbnail
Nate Cavanaugh, modifié il y a 13 années.

RE: Update session variable using Ajax

Junior Member Publications: 94 Date d'inscription: 27/11/06 Publications récentes
Hi Nate,
We have something pretty close, but I don't know if it's 100% what you want. It might get you part of the way there.

We have something that's called session_clicks, but it's actually persisted across sessions (it's kind of like setting a user preference).

What you could do is set this on close, and then on login reset it back to true.

Here's how you could do this:

onClose: function(){
	A.io.request(
		themeDisplay.getPathMain() + '/portal/session_click',
			{
				data: {
					'showPasswordExpirationNotice': false
				}
			}
	);
}


Though you would just need to make sure that somewhere in your code, on login, you're setting:

SessionClicks.put(request, 'showPasswordExpirationNotice', Boolean.FALSE.toString());


Does that make sense?

Its a slightly different way to do it, but otherwise, you would need to wireup you're own class.

Let me know if you have any other questions.

Thanks Nate,
Nate Shaw, modifié il y a 13 années.

RE: Update session variable using Ajax

Junior Member Publications: 49 Date d'inscription: 26/10/10 Publications récentes
Thanks Nate. I am a bit confused though. Are you saying that I have to set the session variable on login so that it exists and can be set later by the Ajax call?

Would I get at this session variable through the usual method, i.e.


$request.getSession().getAttribute("showPasswordExpirationNotice"))


Also, I assume you meant to set the var to true below, not false?


SessionClicks.put(request, 'showPasswordExpirationNotice', Boolean.FALSE.toString());


Thanks for the help!
Nate Shaw, modifié il y a 13 années.

RE: Update session variable using Ajax

Junior Member Publications: 49 Date d'inscription: 26/10/10 Publications récentes
I cannot get this to work.

We are setting "showPasswordExpirationNotice" on user log-in to "true". I tried using both false and 'false' below as the value for "showPasswordExpirationNotice" that is sent by the Ajax call and it does not make a difference. The Ajax call seems to be sent properly according to what I see in Firebug (no errors), but $showNotice is always true in my debugging output. As you can see, I also tried getting "showPasswordExpirationNotice" out of the HttpSession, but that does not work either.


##set($showNotice = $request.getSession().getAttribute("showPasswordExpirationNotice"))
#set($showNotice = $sessionClicks.get($request,"showPasswordExpirationNotice",""))

new Liferay.Notice({
    content: "Your password will expire in ${passwordExpiresDays} days! <a href="$resetPasswordUrl" target="_blank">Reset it now</a> ",
    noticeClass: '$noticeClass',
    toggleText: false,
    type: "Test",
    closeText: "Dismiss",
    onClose: function(){
        //use Ajax to set "showPasswordExpirationNotice" session variable to false
	A.io.request(
	    themeDisplay.getPathMain() + '/portal/session_click',
	    {
	        data: {
                    'showPasswordExpirationNotice': 'false'
		}
	    }
 	);
    }
});

show notice: ${showNotice}


I am not sure what I am doing wrong, if anything and do not see any documentation on session clicks anywhere, so I can't really do much else on this myself. I appreciate any insight you all can provide!
thumbnail
Kyrre Myrbostad, modifié il y a 12 années.

RE: Update session variable using Ajax

Junior Member Publications: 37 Date d'inscription: 21/01/09 Publications récentes
Hi,
any progress on this? I'm trying the same aproach as you but I'm having trouble setting the value on login. Using a PostLoginHook and the following code:

SessionClicks.put(request, "current_role", "Ansatt");


this generates a nullpointer exeption when the SessionClicks class is trying to get the portalpreferences. Did you use another method to set the value on login?
Nate Shaw, modifié il y a 12 années.

RE: Update session variable using Ajax

Junior Member Publications: 49 Date d'inscription: 26/10/10 Publications récentes
I could never get this to work, so I ended up using a cookie instead to store the state I wanted to track.
thumbnail
Bradley Wood, modifié il y a 9 années.

RE: Update session variable using Ajax

New Member Publications: 3 Date d'inscription: 29/07/14 Publications récentes
@Nate did anything change for Liferay 6.2 for session clicks?
thumbnail
David H Nebinger, modifié il y a 9 années.

RE: Update session variable using Ajax

Liferay Legend Publications: 14915 Date d'inscription: 02/09/06 Publications récentes
Seems like this support was removed some time ago, there's no reference to a SessionClick javascript object that I can find.