Velocity (VM) template request parameters: Getting GET variables

。_饼干妹妹 提交于 2019-12-08 09:31:12

问题


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>

回答1:


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)



回答2:


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>



回答3:


$request.getParameter("parameterName")


来源:https://stackoverflow.com/questions/8417505/velocity-vm-template-request-parameters-getting-get-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!