How to get URL query parameters in freemarker template

Hello everyone,
May I know whether I can get URL query parameters directly in freemarker template or not? For example, if I access a URL like http://localhost:8080/site/news/the-first-news.html?param1=value1, I want to get the value of param1 in in freemarker template. I know there’s method to set the parameters using hst:param tag in the template, but how can I get it?

1 Like

Hello Tonny,

can you try something like this in your freemarker {hstRequest.request.getParameter("param1")} this will return value1. Another option is {hstRequest.queryString} this will return param1=value1 and any other parameter that you pass in case you have multiple or if the retrieval of the key is also required.

Hope this helps, kind regards

I think any of these will work:

<@hst.defineObjects/>

${hstRequestContext.servletRequest.getParameter("param1")}
or
${hstRequestContext.servletRequest.parameterMap["param1"]}

Woonsan

1 Like

You should be able to doe hstRequest.getParameter(“param1”) directly. The request object should be available on a JSP in any case, so you can also do request.getParameter.

By default, it wouldn’t return the value as HstRequest reads only its namespace prefixed parameters only.

And apparently ftl doesn’t have an implicit request object.

I’ve tried these methods, and the result is as follows:

  1. ${hstRequest.request.getParameter("param1")} --> OK

  2. ${hstRequestContext.servletRequest.getParameter("param1")} --> OK

  3. ${hstRequestContext.servletRequest.parameterMap["param1"]} --> NG

  4. ${hstRequest.getParameter("param1")} --> NG

  5. ${request.getParameter("param1")} --> NG

Thanks a lot for all your help.