« Back

Session Attributes from Journal Content and Velocity

Company Blogs November 30, 2007 By James Min Staff

Need to access session attributes from Velocity?

 

Well, from a Velocity theme (*.vm), you can simply do something like:

 

$request.getAttribute("USER_ID")

 

or...

 

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

 

 

However, what about from a Velocity template in a Journal Content Portlet? Well, thanks to Bchan and Ray Augé, we have LEP-4377 and LEP-4378. Thanks, Bchan! Thanks, Ray!

 

Some examples of how to do this in a VM template are (from LEP-4378):



- get the portlet-namespace
#set ($namespace = $request.get('portlet-namespace'))

- get a request parameter
#set ($someParameter = $request.get('parameters').get('some-parameter'))

If the parameter has an array or values (checkbox, etc..) the values are in a list.. (because VM doesn't have direct array access)

- get an element from the request param array
#set ($someParameter1 = $request.get('parameters').get('some-parameter-array').get(0))

- get request attribute
#set ($friendlyURL = $request.get('attributes').get('FRIENDLY_URL'))

- get a portlet session attribute
#set ($somePSAttribute = $request.get('portlet-session').get('portlet-attributes').get('com.liferay.util.servlet.SessionMessages'))

- get an application session attribute
#set ($someASAttribute = $request.get('portlet-session').get('application-attributes').get('LIFERAY_SHARED_VISITED_GROUP_ID_RECENT'))

 

And there you have it. Check out the LEP's for the patches and versions affected.

 

 

Threaded Replies Author Date
I just updated the Wiki with and example of how... Ray Augé November 30, 2007 10:55 AM
awesome James Min September 26, 2008 8:23 PM
hai Ray, Could you please assist me in... Sharath Babu A March 2, 2010 3:08 AM
$request.portlet-session gets you the session... Ray Augé June 12, 2010 8:05 AM
Hi Ray! Sorry for posting in a wrong thread.... Christian De Vera Talampas June 9, 2010 1:04 AM
Arrays are handled automatically in Velocity. ... Ray Augé June 12, 2010 7:58 AM
thanks ray! however i have other queries. i... Christian De Vera Talampas June 15, 2010 3:08 AM
The only way that I have found to do this in... Ray Augé September 27, 2010 10:07 AM
ray plz help me to get user id in a simple jsp... prabhakar m February 25, 2011 7:24 AM
request.getRemoteUser() Ray Augé February 25, 2011 7:37 AM
while im useing <%=request.getRemoteUser()%> ... prabhakar m February 27, 2011 8:24 PM
Please note that the request must pass through... Ray Augé February 27, 2011 9:48 PM
thanks mr. ray.. i got the user attributes in... prabhakar m February 28, 2011 11:04 PM
I'm struggling with arrays in Velocity,... jeff gerbracht August 5, 2010 12:08 PM
Hi All, I created a custom.jsp file in which I... Kuldeep Bhati April 25, 2011 3:13 AM
Hi Simon, I am trying to do the same as you in... David García González June 16, 2011 3:29 AM
Hi Ray , is it working in liferay 6 ?? I am... KK rajput August 9, 2011 2:09 AM
Since you didn't specify the scope for the... Ray Augé August 15, 2011 9:10 AM
Is there a way to access the action phase from... Max Gabrielsson September 1, 2011 1:59 AM
How can we get httpsession in web content VM... suresh yadagiri December 10, 2013 11:56 AM
You can't! You can only get a readonly map... Ray Augé December 10, 2013 1:14 PM
okay. we need to access some data (ex:... suresh yadagiri December 10, 2013 1:26 PM

I just updated the Wiki with and example of how this might be used from either XSL or VM templates.

<a href="http://wiki.liferay.com/index.php/Journal_Internal_API#Request_Handling_Ex­ample">http://wiki.liferay.com/index.php/Journal_Internal_API#Request_Handling_E­xample</a>

Enjoy!
Posted on 11/30/07 10:55 AM.
Posted on 9/26/08 8:23 PM in reply to Ray Auge.
hai Ray,

Could you please assist me in retrieving the portletsession variable kept in jsp into .vm file. I tried in the below fashion but unable to get the value..

#set ($somePSAttribute = $request.get('portlet-session').get('keyname').get('com.liferay.util.servlet.Ses­sionMessages'))

or i need to write an additional code in any .xml files?
Posted on 3/2/10 3:08 AM in reply to Ray Auge.
Hi Ray!

Sorry for posting in a wrong thread. Do you have example on how to initialize and manipulate arrays in velocity?

Thanks
Posted on 6/9/10 1:04 AM in reply to Ray Auge.
Arrays are handled automatically in Velocity.

this is how you create an empty array: #set ($arr = []) a non-empty array: #set ($arr2 = [1, 2, 3])

But whenever you think "array", you should really think "ArrayList", which means that manipulation uses the java.util.List interface: $arr2.add(4), $arr2.size(), etc

But where you are passing this to a method call that really requires an array, velocity will autobox down from ArrayList to plain array, as long as the contents of the ArrayList fit the parameter array type of the method.
Posted on 6/12/10 7:58 AM in reply to Christian De Vera Talampas.
$request.portlet-session

gets you the session Map. but there are two paths from here: portlet-attributes, and application-attributes to emulate the PortletSession.PORTLET_SCOPE and PortletSession.APPLICATION_SCOPE respectively.

Therefore you would have something like:

#set ($attribute = $request.portlet-session.portlet-attributes.keyname)

Note: the returned type is always String, so you may have to parse that result of the object was serializable, but not a String or primitive or primitive Wrapper class object.
Posted on 6/12/10 8:05 AM in reply to simon tuffle.
thanks ray! however i have other queries. i tried to use this line $arr2.add(1) but i noticed that it returns a value "true" (probably a flag). how can i prevent that from happening? i just want to insert an element and access it. plus if i want to access an array element of a certain index, how do i do that?
Posted on 6/15/10 3:08 AM in reply to Ray Augé.
I'm struggling with arrays in Velocity, specifically, I can't get the method TagsEntryLocalService.getEntryIds() to work though getEntry works fine. I've the following code in a template (note that in reality, each #set is on it's own line)

#set( $tags=["gallery"])
#set ($tagsEntryService= $serviceLocator.findService("com.liferay.portlet.tags.service.TagsEntryLocalServ­ice"))
#set ($entries=$tagsEntryService.getEntryIds($getterUtil.getLong($companyId),$tags))
#­set ($entryid=$tagsEntryService.getEntry($getterUtil.getLong($companyId),$tags.get(0­)).getEntryId())
tags=$tags
entries = $entries
entryID=$entryid

which displays

tags= entries = $entries entryID=717820
and it should be something like
tags= entries = [717820] entryID=717820

Any Ideas???
Posted on 8/5/10 12:08 PM in reply to Ray Augé.
The only way that I have found to do this in velocity was to fudge a void method call using #set:

#set ($void = $list.add($thing))
Posted on 9/27/10 10:07 AM in reply to Christian De Vera Talampas.
ray plz help me to get user id in a simple jsp portlet... im very new to liferay.. plz help me
Posted on 2/25/11 7:24 AM in reply to Ray Augé.
request.getRemoteUser()
Posted on 2/25/11 7:37 AM in reply to prabhakar m.
while im useing <%=request.getRemoteUser()%> im getting null value ...
Posted on 2/27/11 8:24 PM in reply to Ray Augé.
Please note that the request must pass through the portal in order to get the proper result.

A direct request such as /context-portlet/my.jsp will not work.
Posted on 2/27/11 9:48 PM in reply to prabhakar m.
thanks mr. ray.. i got the user attributes in my jsp portlet.. thank you so much...
Posted on 2/28/11 11:04 PM in reply to Ray Augé.
Hi All,

I created a custom.jsp file in which I have simple HttpSession variable as below

<code>
session.setAttribute("loginname", loginname);
</code>

My objective is to display this loginname session variable in my portal_normal.vm file,

I have also tried using the following way,

<code>
#set( $session = $request.getSession(true) )
#set( $name = $session.getAttribute(loginname) )
$name
</code>

But still no idea, how to access HttpSession data in the velocity file, Please help on this.
Posted on 4/25/11 3:13 AM.
Hi Simon, I am trying to do the same as you in Liferay 6, but I am unable to get it.

Did you finally get the portlet variable you needed?
Thanks!
Posted on 6/16/11 3:29 AM in reply to Kuldeep Bhati.
Hi Ray ,
is it working in liferay 6 ?? I am unable to make it work ?? Below is my code
in Jsp :
renderRequest.getPortletSession().setAttribute("attr1", "attr1");

In vm :
#set ($customAttribute = $request.getSession().getAttribute("attr1"))
$customAttribute

#set ($somePSAttribute = $request.get('portlet-session').get('portlet-attributes').get('attr1'))

$somePSAt­tribute

#set ($someASAttribute = $request.get('portlet-session').get('application-attributes').get('attr1'))

$some­ASAttribute

but none is working
Posted on 8/9/11 2:09 AM in reply to David García González.
Since you didn't specify the scope for the attribute, it will be a portlet attribute value and only the SAME portlet will be able to see it, therefore the web content portlet won't be able to see it.

To share the attribute you need to set it in the application scope:

renderRequest.getPortletSession().setAttribute("attr1", "attr1", PortletSession.APPLICATION_SCOPE);

then you'll be able to see it like this:

#set ($somePSAttribute = $request.portlet-session.application-attributes.attr1)
Posted on 8/15/11 9:10 AM in reply to KK rajput.
Is there a way to access the action phase from a journal article. So I can set a session variable and other portlets will render accordingly?

The scope would be a mvc portlet that shows detail for an Item which listens to a session variable. I want to be able to do a journal article that shows a link to the item and the details portlet will render that item from the session variable.
Posted on 9/1/11 1:59 AM.
How can we get httpsession in web content VM template?
Posted on 12/10/13 11:56 AM.
You can't! You can only get a readonly map which represents any trivially serializable contents it contains.
Posted on 12/10/13 1:14 PM in reply to suresh yadagiri.
okay. we need to access some data (ex: EmployeeDetails Bean) we get in AutoLogin hook inside content display portlet. As we have only HttpRequest and HttpResponse in Auto Login hook, i am not sure how to send data to VM template. any suggestions?
Posted on 12/10/13 1:26 PM.