速度(VM)模板请求参数:获取GET变量 [英] Velocity (VM) template request parameters: Getting GET variables

查看:282
本文介绍了速度(VM)模板请求参数:获取GET变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问VM模板中URI中传递的GET变量?

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

这仅在加载窗口小部件URL时有效:

This works only when loading the widget URL:

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

我正在寻找一种适用于友好URL的简洁解决方案.

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

这是我的测试模板:

<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>

推荐答案

另一种选择是使用$ httpUtil,这是在Velocity模板中注入的Liferay实用程序类.这样,您可以执行以下操作:

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)

这篇关于速度(VM)模板请求参数:获取GET变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆