留言板

how to get request parameters in velocity template

thumbnail
Jerry Liu,修改在10 年前。

how to get request parameters in velocity template

Junior Member 帖子: 47 加入日期: 13-10-29 最近的帖子
hi everybody!
as follows code
url:
localhost:8080/web/p01/tesat?n=23


#set($n = $request.getParameter('n'))
#set($n2 = $request.get('n'))
#set($n3 = $request.get("parameters").get("n"))
#set($n4 = $paramUtil.getLong($request,'n'))
#set($url = $request.attributes.CURRENT_URL)
#set($singleValue = $httpUtil.getParameter($url, 'n', false))
#set($n5 = $httpUtil.getParameterMap($httpUtil.getQueryString($url)).n)
#set ($current_url = $request.get("attributes").CURRENT_COMPLETE_URL)
#set ($paramName='n')
#set ($n6 = $httpUtil.getParameter($current_url, $paramName))


n:$n  <br>
n2:$n2  <br>
n3:$n3  <br>
n4:$n4  <br>
n5:$n5 <br>
n6:$n6 <br>



But nothing was set.
What am I doing wrong?
thanks.
thumbnail
Jerry Liu,修改在10 年前。

RE: how to get request parameters in velocity template

Junior Member 帖子: 47 加入日期: 13-10-29 最近的帖子
meera prince:
Hi
You have covered all scenarios so one of should work..
Go through following links it mat help you..
http://stackoverflow.com/questions/8417505/velocity-vm-template-request-parameters-getting-get-variables

http://stackoverflow.com/questions/9531726/get-parameters-from-velocity-template

https://www.liferay.com/community/forums/-/message_boards/message/2304909

Regards,
Meera Prince


thanks Meera.
I've seen these,but there is no success.
thumbnail
Tejas Kanani,修改在10 年前。

RE: how to get request parameters in velocity template

Liferay Master 帖子: 654 加入日期: 09-1-6 最近的帖子
#set ($current_url = $request.get("attributes").CURRENT_COMPLETE_URL)
#set ($paramName='n')
#set ($n6 = $httpUtil.getParameter($current_url, $paramName))

I've used this earlier and that worked for me.

Can you try below code which would print each value which helps to resolve what is not working ?
Request : $request
#set ($current_url = $request.get("attributes").CURRENT_COMPLETE_URL)
Current URL : $current_url
#set ($paramName='n')
HttpUtil : $httpUtil
#set ($n6 = $httpUtil.getParameter($current_url, $paramName))
n6 : $n6
thumbnail
Jerry Liu,修改在10 年前。

RE: how to get request parameters in velocity template

Junior Member 帖子: 47 加入日期: 13-10-29 最近的帖子
I have been solved
as follows.
  #set ($serviceContext = $portal.getClass().forName("com.liferay.portal.service.ServiceContextThreadLocal").getServiceContext())
#set ($httpServletRequest = $serviceContext.getRequest())
#set($seminarId =$paramUtil.getLong($httpServletRequest, 'seminarId'))
maria pavlova,修改在10 年前。

RE: how to get request parameters in velocity template

New Member 帖子: 6 加入日期: 13-5-6 最近的帖子
Does the request object need to be defined in a .vm file?

if I dump $request, it shows up as $request, same for any parameters in it.

thanks for any help
thumbnail
Baladhandapani Nagarajan,修改在9 年前。

RE: how to get request parameters in velocity template

Junior Member 帖子: 26 加入日期: 15-1-12 最近的帖子
Hi All ,
Iam facing similar type of issue , can some one please help me here - We are in the process of migrating our project from 6.1 to 6.2

In my VM file : (This VM file is part of theme, We have embeded portlets as part of theme. Basically we are trying to read the 'contentName' parameter in the controller class and get the corresponding content from the documentum server to display it as part of embeded portlet.
----------------
Entry in VM file

$theme.runtime("embeddedWebContent_WAR_wp_INSTANCE_ftr40", "contentName=abc", "")

In my controller class
String contentName = request.getParameter("contentName");

Iam not getting the value "abc" in my contoller class , this code was working fine with 6.1 and this happening after our migration. The strange part is , If i create a new page and apply the same theme , then Iam getting the request parameter value in the controller class. (This is failing only for the existing pages and working fine with 6.1)

We cannot delete and reapply the theme for all the pages in production - I really appreciate your inputs !
thumbnail
David H Nebinger,修改在9 年前。

RE: how to get request parameters in velocity template

Liferay Legend 帖子: 14915 加入日期: 06-9-2 最近的帖子
This came up recently, almost exactly the same as last time...

contentName is not namespaced so it will not be part of the portlet request parameters. You have to get the HTTP request and pull the parameter from there.
thumbnail
Baladhandapani Nagarajan,修改在9 年前。

RE: how to get request parameters in velocity template

Junior Member 帖子: 26 加入日期: 15-1-12 最近的帖子
Thank you David , let me try and update you.
thumbnail
Baladhandapani Nagarajan,修改在9 年前。

RE: how to get request parameters in velocity template

Junior Member 帖子: 26 加入日期: 15-1-12 最近的帖子
Hi David,

I tried converting to HttpRequest

HttpServletRequest originalRequest = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(request));
HttpServletRequest httpRequest = themeDisplay.getRequest();

String conName = httpRequest.getParameter("contentName");
String param = originalRequest.getParameter("contentName");

Still 'contentName' in the request is null.Again I want to insist that the same request parameter is not empty for the new applied theme page , the issue is happening only for the existing pages (request parameter content name is coming as null for the already existing pages)

Do I need to set the content Name prefixed with <portletnamespace/> in the VM file before I access in Controller class ?
thumbnail
David H Nebinger,修改在9 年前。

RE: how to get request parameters in velocity template

Liferay Legend 帖子: 14915 加入日期: 06-9-2 最近的帖子
This argument makes no sense. A legacy page, one pre-upgrade, does not have the theme frame around it stored anywhere. It is still built by the portal using the available theme tied to the page at runtime.

Are you sure you're using the same theme on the legacy pages as the new pages?
thumbnail
Baladhandapani Nagarajan,修改在9 年前。

RE: how to get request parameters in velocity template

Junior Member 帖子: 26 加入日期: 15-1-12 最近的帖子
Yes I agree , Iam using the same theme for the legacy (pre-upgrade) as well as the newly created blank page. I reapplied different themes on the newly created page , I don't face any issue in getting the request parameter from the VM file.
thumbnail
Baladhandapani Nagarajan,修改在9 年前。

RE: how to get request parameters in velocity template

Junior Member 帖子: 26 加入日期: 15-1-12 最近的帖子
In this case (for the legacy pages) its forwarding to virtual page , that may be the reason Iam not getting directly from the portlet / http request. Iam planning to get the value from 'PortletPreference' object instead of request - Can some one suggest me to set the content name value in 'Portlet Preference' object in VM file and logic to retrieve it from the controller class.

I modified the entry in VM file / contoller , still Iam not able to get the 'contentName' value :


#set ($VOID = $velocityPortletPreferences.setValue("contentName", "abc"))
$theme.runtime("embeddedWebContent_WAR_wp_INSTANCE_ftr40", "contentName=abc", $velocityPortletPreferences.toString())
#set ($VOID = $velocityPortletPreferences.reset())

In my controller class
Enumeration names = preferences.getNames();

Iterated the 'names' , it doesn't printed out the content name - am I missing something ?
thumbnail
Julius Garcia,修改在8 年前。

RE: how to get request parameters in velocity template

New Member 发布: 1 加入日期: 15-8-3 最近的帖子
Here's a very simple solution...

e.g: .../?param1=Hello%20World
vm:
#set ($param1 = $request.getParameter('param1'))

##$param1 will have "Hello World" value
thumbnail
Fernando Fernandez,修改在7 年前。

RE: how to get request parameters in velocity template

Expert 帖子: 396 加入日期: 07-8-22 最近的帖子
This page says $request is a javax.servlet.http.HttpServletRequest but this might cause some confusion because $request, in CMS templates is, in fact, an HashMap with lots of different objects of different classes.

If you want to find the URL with the parameters you can use:

$request.get('attributes').get('CURRENT_URL')


HTH

Fernando