9/14/2015

How to read URL Parameters in Freemarker


I found an interesting snippet today while I was searching for a way to read URL Parameters in Freemarker Asset Publisher Templates. What I wanted to do was to configure the way my Asset Publisher Advanced Display Template behaves. I am creating URLs like this : http://www.my-server.com?param=value and I wanted to read them in an Asset Publisher freemarker template.
This is how you do it :


<#assign context = staticUtil["com.liferay.portal.service.ServiceContextThreadLocal"].getServiceContext()>
<#assign httpServletRequest = context.getRequest()>
<#assign parameter = (httpServletRequest.getParameter('param')?html)!''>


The interesting part is the !' ' at the end which prevents Exceptions from popping up when there is no URL parameter.