Vue combinée Vue Plate Arborescence
Sujets [ Précédent | Suivant ]
toggle
Nate Shaw
Update session variable using Ajax
26 octobre 2010 06:25
Réponse

Nate Shaw

Rang: Junior Member

Publications: 37

Date d'inscription: 26 octobre 2010

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.

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


Thanks for the help!
Nate Cavanaugh
RE: Update session variable using Ajax
28 octobre 2010 09:54
Réponse

Nate Cavanaugh

LIFERAY STAFF

Rang: Junior Member

Publications: 87

Date d'inscription: 27 novembre 2006

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:

 1onClose: function(){
 2    A.io.request(
 3        themeDisplay.getPathMain() + '/portal/session_click',
 4            {
 5                data: {
 6                    'showPasswordExpirationNotice': false
 7                }
 8            }
 9    );
10}


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

1SessionClicks.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
RE: Update session variable using Ajax
28 octobre 2010 12:25
Réponse

Nate Shaw

Rang: Junior Member

Publications: 37

Date d'inscription: 26 octobre 2010

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.

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


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

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


Thanks for the help!
Nate Shaw
RE: Update session variable using Ajax
1 novembre 2010 08:59
Réponse

Nate Shaw

Rang: Junior Member

Publications: 37

Date d'inscription: 26 octobre 2010

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.

 1
 2##set($showNotice = $request.getSession().getAttribute("showPasswordExpirationNotice"))
 3#set($showNotice = $sessionClicks.get($request,"showPasswordExpirationNotice",""))
 4
 5new Liferay.Notice({
 6    content: "Your password will expire in ${passwordExpiresDays} days! <a href='$resetPasswordUrl' target='_blank'>Reset it now</a> ",
 7    noticeClass: '$noticeClass',
 8    toggleText: false,
 9    type: "Test",
10    closeText: "Dismiss",
11    onClose: function(){
12        //use Ajax to set "showPasswordExpirationNotice" session variable to false
13    A.io.request(
14        themeDisplay.getPathMain() + '/portal/session_click',
15        {
16            data: {
17                    'showPasswordExpirationNotice': 'false'
18        }
19        }
20     );
21    }
22});
23
24show 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!
Kyrre Myrbostad
RE: Update session variable using Ajax
13 octobre 2011 13:33
Réponse

Kyrre Myrbostad

Rang: Junior Member

Publications: 37

Date d'inscription: 21 janvier 2009

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:

1SessionClicks.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
RE: Update session variable using Ajax
14 octobre 2011 08:30
Réponse

Nate Shaw

Rang: Junior Member

Publications: 37

Date d'inscription: 26 octobre 2010

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.
Bradley Wood
RE: Update session variable using Ajax
25 août 2014 08:16
Réponse

Bradley Wood

Rang: New Member

Publications: 3

Date d'inscription: 29 juillet 2014

Publications Récentes

@Nate did anything change for Liferay 6.2 for session clicks?
David H Nebinger
RE: Update session variable using Ajax
25 août 2014 09:00
Réponse

David H Nebinger

Community Moderator

Rang: Liferay Legend

Publications: 11770

Date d'inscription: 1 septembre 2006

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.