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.

