Foren

how to set session variable in velacity templates

ruchi sharma, geändert vor 12 Jahren.

how to set session variable in velacity templates

Junior Member Beiträge: 95 Beitrittsdatum: 24.03.11 Neueste Beiträge
Hello,
I am trying to set session variable as mentioned below in my velocity template in theme,
$request.getSession().setAttribute("currentUrl", $currentUrl);
also tried
$session.setAttribute("currentUrl", $currentUrl);

I have tried to print the value of $request.getSession() and i am not getting null in my velocity template.
When i try to read the session variable e.g request.getsession(); in my portlet i dint get the value (getting null).
Is it correct way to do this or any other way to do it.
Thanks is advance.
Regards
Ruchi
thumbnail
Filip Rak, geändert vor 12 Jahren.

RE: how to set session variable in velacity templates

New Member Beiträge: 15 Beitrittsdatum: 10.02.10 Neueste Beiträge
Hi,
I don't think it's possible without a hack. It's a security measure not give HttpSession, nor HttpRequest to content creators. YOU HAVE BEEN WARNED. If you are sure what you are doing add custom code to VelocityVariables, put un-wrapped session to variables accessible from velocity, also template should not be cached.
ruchi sharma, geändert vor 12 Jahren.

RE: how to set session variable in velacity templates

Junior Member Beiträge: 95 Beitrittsdatum: 24.03.11 Neueste Beiträge
Hello Filip,
Thanks for the reply . Could you please give me an example, I dint get it what you are trying to say.
Thanks
Ruchi
thumbnail
Raja Nagendra Kumar, geändert vor 12 Jahren.

RE: how to set session variable in velacity templates

Expert Beiträge: 484 Beitrittsdatum: 02.03.06 Neueste Beiträge
Portlet Session (I am hoping that you are referring to HTTPSession) has no meaning in the context of velocity template.. as template applies to whole page and portlet session applies to each portlet..

The approach you should take is define custom velocity variables in the theme and try to use

req.getAttribute(WebKeys.VM_VARIABLES) to get map of velocity variables

Look for more details at

http://www.liferay.com/community/wiki/-/wiki/Main/Custom+Velocity+Variables
http://apoorvaprakash.blogspot.com/2011/09/available-implicit-objects-liferay-jsp.html

Regards,
Raja Nagendra Kumar,
C.T.O
www.tejasoft.com
-Liferay Adoption Excellence
ruchi sharma, geändert vor 12 Jahren.

RE: how to set session variable in velacity templates

Junior Member Beiträge: 95 Beitrittsdatum: 24.03.11 Neueste Beiträge
Thank you very much.. that was really helpful.
I have tried this and its working fine for me .
But still I am having one problem,
Like, in my velocity template i am having following variable
$currentURL= "some value";

and I want to set the value of this variable($currentURL) in velocity variables map as below,

Map<String, Object> vmVariables = new HashMap<String, Object>();
vmVariables.put("CURRENTURL", currentURL); // how to get value of custome velocity variable in my class.
request.setAttribute(WebKeys.VM_VARIABLES, vmVariables);

Please could you let me know how to get the custom velocity variablel value .

Thanks
Ruchi
thumbnail
Raja Nagendra Kumar, geändert vor 12 Jahren.

RE: how to set session variable in velacity templates

Expert Beiträge: 484 Beitrittsdatum: 02.03.06 Neueste Beiträge
Can u check with in portlet render or action method

renderRequest.getAttribute(WebKeys.VM_VARIABLES)
or actionReques.getAttribute(WebKeys.VM_VARIABLES)

not be null..

Else, try to do settings of the same ActionHook as described in http://www.liferay.com/community/wiki/-/wiki/Main/Custom+Velocity+Variables

Regards,
Raja Nagendra Kumar,
C.T.O
www.tejasoft.com
thumbnail
Puneet Upadhyay, geändert vor 12 Jahren.

RE: how to set session variable in velacity templates

Regular Member Beiträge: 234 Beitrittsdatum: 22.10.11 Neueste Beiträge
Hi Nagendra !!!
Please let me know that where i can find the velocity template documentation for enhancing my knowledge.
Thanks
thumbnail
Raja Nagendra Kumar, geändert vor 12 Jahren.

RE: how to set session variable in velacity templates

Expert Beiträge: 484 Beitrittsdatum: 02.03.06 Neueste Beiträge
You can get the detailed info on velocity from Apache project site http://velocity.apache.org/

Regards,
Raja Nagendra Kumar
thumbnail
Puneet Upadhyay, geändert vor 12 Jahren.

RE: how to set session variable in velacity templates

Regular Member Beiträge: 234 Beitrittsdatum: 22.10.11 Neueste Beiträge
Thanks Nagendra !!!
But i need the links which give the information velocity template with liferay. emoticon
ruchi sharma, geändert vor 12 Jahren.

RE: how to set session variable in velacity templates

Junior Member Beiträge: 95 Beitrittsdatum: 24.03.11 Neueste Beiträge
Hello,

req.getAttribute(WebKeys.VM_VARIABLES) is not null.

Map<String, Object> vmVariables = new HashMap<String, Object>();
vmVariables.put("CURRENTURL", currentURL); // how to get value of custome velocity variable in my class.
request.setAttribute(WebKeys.VM_VARIABLES, vmVariables); // i have set this map in velocityvariables.java class

Here in this map I want to put the value of custome velocity variable value as the value (vmVariables.put("CURRENTURL", currentURL)) , which is in my velocity template
Like, in my velocity template i am having following variable
$currentURL= "some value";
I want to get the $currentURL value in the class where I am setting the map.
Simply I mean,how to access these custome velocity variable value in portlet class ..code snipped would help .
Thanks
Ruchi
ruchi sharma, geändert vor 12 Jahren.

RE: how to set session variable in velacity templates

Junior Member Beiträge: 95 Beitrittsdatum: 24.03.11 Neueste Beiträge
I am trying to do like this , probably form velocity context i'll get some thing.
ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
template = Velocity.getTemplate(themeDisplay.getPathThemeTemplates()+"/portal_normal.vm");

But I am getting this exception
java.lang.NoClassDefFoundError: Could not initialize class org.apache.velocity.runtime.RuntimeSingleton
and sometime this exception
java.lang.NoClassDefFoundError: org/apache/commons/collections/ExtendedProperties