Session Attributes from Journal Content and Velocity

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.

 

 

Blogs
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_Example">http://wiki.liferay.com/index.php/Journal_Internal_API#Request_Handling_Example</a>

Enjoy!
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.SessionMessages'))

or i need to write an additional code in any .xml files?
$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.
Hi Ray!

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

Thanks
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.
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?
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))
ray plz help me to get user id in a simple jsp portlet... im very new to liferay.. plz help me
request.getRemoteUser()
while im useing <%=request.getRemoteUser()%> im getting null value ...
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.TagsEntryLocalService"))
#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=[gallery] entries = $entries entryID=717820
and it should be something like
tags=[gallery] entries = [717820] entryID=717820

Any Ideas???
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.
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!
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'))

$somePSAttribute

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

$someASAttribute

but none is working
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)
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.
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?