سؤال

How do i access GET variables as passed in the URI in a VM template?

This works only when loading the widget URL:

$request.get("parameters").get("fav").get(0)

I'm looking for a neat solution that works with friendly URLs.

Here's my testing template:

<br><br><br>
#set($url = $request.attributes.CURRENT_URL)

<h2>url: $url</h2>

#set($favs = $url.split("fav="))
favs: $favs<br>
favs.size(): $favs.size() <br>
#if($favs.size() > 1)
  #set($fav1 = $favs.get(1).split("&").get(0))
  fav1: $fav1<br>
#else
  No fav!
#end
#if($favs.size() > 2)
  #set($fav2 = $favs.get(2).split("&").get(0))
  fav2: $fav2<br>
#end

#set($favs2 = $httpUtil.getParameterMap($url, "fav"))
favs2: $favs2

<hr>
<h3>Fav?</h3>
<form method="get">
  <input type="checkbox" name="fav" value="dave"/> Dave<br>
  <input type="checkbox" name="fav" value="nate"/> Nate<br>
  <input type="checkbox" name="fav" value="taylor"/> Taylor<br>
  <input type="submit" value="Send"/>
</form>
<hr>
<div style="font-size: 9px;">request: $request</div>
هل كانت مفيدة؟

المحلول

Another option would be to use $httpUtil, a Liferay utility class that's injected in Velocity templates. This way you could do the following:

#set($url = $request.attributes.CURRENT_URL)
#set($singleValue = $httpUtil.getParameter($url, "foo", false))
#set($multipleValues = $httpUtil.getParameterMap($httpUtil.getQueryString($url)).foo)

نصائح أخرى

I couldn't find the supposed solution in the terrible documentation, so i came up with this:

#set($curl = $request.get("attributes").get("CURRENT_URL"))
#set($foo = $curl.split("foo="))
<hr>
#if($foo.size() > 1)
  #set($foo1 = $foo.get(1).split("&").get(0))
  foo1: $foo1
#end
<hr>
#if($foo.size() > 2)
  #set($foo2 = $foo.get(2).split("&").get(0))
  foo2: $foo2
#end
<hr>
$request.getParameter("parameterName")
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top