掲示板

how to convert long type value to string type in velocity template?

thumbnail
11年前 に maryam maryam masoudy によって更新されました。

how to convert long type value to string type in velocity template?

Regular Member 投稿: 149 参加年月日: 11/10/30 最新の投稿
hi anyone
how to convert long type value to string type in velocity template?
thanks a lot
thumbnail
11年前 に Paul . によって更新されました。

RE: how to convert long type value to string type in velocity template?

Liferay Master 投稿: 522 参加年月日: 11/08/29 最新の投稿
You can use $getterUtil.getString(..)
thumbnail
11年前 に maryam maryam masoudy によって更新されました。

RE: how to convert long type value to string type in velocity template?

Regular Member 投稿: 149 参加年月日: 11/10/30 最新の投稿
paul thanks for reply

but i think $getterUtil.getString(..) get string and return string !!

添付ファイル:

thumbnail
11年前 に Paul . によって更新されました。

RE: how to convert long type value to string type in velocity template?

Liferay Master 投稿: 522 参加年月日: 11/08/29 最新の投稿
In velocity template? You can use it directly without declaring a variable etc as indicated in the attached image. Also in you question you asked how to get a string value.
$getterUtil.getString(..) takes object and returns a string. The actual method is com.liferay.portal.kernel.util.GetterUtil.getString(Object value)
com.liferay.portal.kernel.util.GetterUtil_IW is wrong
com.liferay.portal.kernel.util.GetterUtil is right

Hope it's clear.
thumbnail
11年前 に maryam maryam masoudy によって更新されました。

RE: how to convert long type value to string type in velocity template?

Regular Member 投稿: 149 参加年月日: 11/10/30 最新の投稿
i know in velociti i can use $getterUtil directly i write in itellij IDE in on sample portlet because i want to certain from code but i think in velocity
getteruttil is in com.liferay.portal.kernel.util.GetterUtil_IW
however i have error when i write $getterUtil.getString(Longvariable)

添付ファイル:

thumbnail
11年前 に Paul . によって更新されました。

RE: how to convert long type value to string type in velocity template?

Liferay Master 投稿: 522 参加年月日: 11/08/29 最新の投稿
Sorry my bad.
I wasn't aware that you were using Liferay 5.2. , I did a quick debug on my Liferay instance which is Liferay 6.1 (this class gets called com.liferay.portal.kernel.util.GetterUtil)
I checked further to get the conversion done and it seems even it's possible to pass any object to get it's string value, but the implementation only returns if the passed object is an instance of String . So I thought of a workaround.
$portal.getClass().forName("java.lang.String").valueOf($group_id)

It may not be the way to do it, but should work. here replace the variable $group_id with your long variable.
Let me know if it works fine.
thumbnail
11年前 に maryam maryam masoudy によって更新されました。

RE: how to convert long type value to string type in velocity template?

Regular Member 投稿: 149 参加年月日: 11/10/30 最新の投稿
hi
this code resolve my problem
$portal.getClass().forName("java.lang.String").valueOf($group_id)

thanks alot very much
after some days i am happy emoticon
9年前 に R DS によって更新されました。

RE: how to convert long type value to string type in velocity template?

Junior Member 投稿: 41 参加年月日: 13/10/15 最新の投稿
maryam maryam masoudy:
hi
this code resolve my problem
$portal.getClass().forName("java.lang.String").valueOf($group_id)

thanks alot very much
after some days i am happy emoticon



How can I do this in Liferay 6.2? Above code doesn't work in LFR 6.2.

Thanks!
thumbnail
9年前 に Jitendra Rajput によって更新されました。

RE: how to convert long type value to string type in velocity template?

Liferay Master 投稿: 875 参加年月日: 11/01/07 最新の投稿
You can use $getterUtil for type conversion ..
use $getterUtil.getString(..)
9年前 に R DS によって更新されました。

RE: how to convert long type value to string type in velocity template?

Junior Member 投稿: 41 参加年月日: 13/10/15 最新の投稿
Jitendra Rajput:
You can use $getterUtil for type conversion ..
use $getterUtil.getString(..)



This is not working for me.
This code
#set( $ricd = $getterUtil.getString($nav_item.getPrimaryKey()) )
	    		<span>x: $getterUtil.getString(10) :x</span>


prints x: :x in my web page.
$nav_item.getPrimaryKey() returns an integer.

Thanks
thumbnail
9年前 に Jitendra Rajput によって更新されました。

RE: how to convert long type value to string type in velocity template?

Liferay Master 投稿: 875 参加年月日: 11/01/07 最新の投稿
It should work . Which version of Liferay you are using . I saw in Liferay 6.2 this class is getting injected from TemplateContextHelper.java
9年前 に R DS によって更新されました。

RE: how to convert long type value to string type in velocity template?

Junior Member 投稿: 41 参加年月日: 13/10/15 最新の投稿
Jitendra Rajput:
It should work . Which version of Liferay you are using . I saw in Liferay 6.2 this class is getting injected from TemplateContextHelper.java



I'm using Liferay 6.2.
I explain my problem: I'm trying to retrieve permissions for an organization's site pages (Layout model). In my Velocity template i use this code
#set ( $hasNotPermission =  $resourcePermissionService.hasResourcePermission($user.getCompanyId(), "com.liferay.portal.model.Layout", $portal.getClass().forName("com.liferay.portal.model.ResourceConstants").SCOPE_INDIVIDUAL, $getterUtil.getString($nav_item.getPrimaryKey()), $userRolesIds, "MY_ACTION_ID") )


In this code I have two problems:
  • $getterUtil.getString($nav_item.getPrimaryKey()): Convert the primary key of the page (layout model) from long to string. $nav_item.getPrimaryKey() correctly returns the id but the whole expression returns blank
  • $portal.getClass().forName("com.liferay.portal.model.ResourceConstants").SCOPE_INDIVIDUAL: I have to refer the static field SCOPE_INDIVIDUAL of the Liferay class ResourceConstants but the whole expression returns "$portal.getClass().forName("com.liferay.portal.model.ResourceConstants").SCOPE_INDIVIDUAL"


Thanks
thumbnail
9年前 に Jitendra Rajput によって更新されました。

RE: how to convert long type value to string type in velocity template?

Liferay Master 投稿: 875 参加年月日: 11/01/07 最新の投稿
I hope you are using findService to locate $resourcePermissionService.

#set ($resourcePermissionService= $serviceLocator.findService("com.liferay.portal.service.ResourcePermissionService"))
9年前 に R DS によって更新されました。

RE: how to convert long type value to string type in velocity template?

Junior Member 投稿: 41 参加年月日: 13/10/15 最新の投稿
Jitendra Rajput:
I hope you are using findService to locate $resourcePermissionService.

#set ($resourcePermissionService= $serviceLocator.findService("com.liferay.portal.service.ResourcePermissionService"))



I'm locating the service with this code:
#set ($resourcePermissionService = $serviceLocator.findService("com.liferay.portal.service.ResourcePermissionLocalService"))
thumbnail
9年前 に Jitendra Rajput によって更新されました。

RE: how to convert long type value to string type in velocity template?

Liferay Master 投稿: 875 参加年月日: 11/01/07 最新の投稿
Have you made any changes in below property pf portal-ext ? .

This is default value of property ..

#
# Set a comma delimited list of variables the Velocity engine cannot
# have access to. This will affect Dynamic Data List templates, Journal
# templates, and Portlet Display templates.
#
velocity.engine.restricted.variables=serviceLocator
9年前 に R DS によって更新されました。

RE: how to convert long type value to string type in velocity template?

Junior Member 投稿: 41 参加年月日: 13/10/15 最新の投稿
My portal-ext doesn't contains the property velocity.engine.restricted.variables.
I tried to add the line velocity.engine.restricted.variables= and now the line velocity.engine.restricted.variables=serviceLocator but nothing change.

But I'm sure that serviceLocator works well because i use other Liferay service and all works well.
For example I successfully use
#set ($layoutLS = $serviceLocator.findService("com.liferay.portal.service.LayoutLocalService"))
#set ($groupService = $serviceLocator.findService("com.liferay.portal.service.GroupService"))
#set ($orgService = $serviceLocator.findService("com.liferay.portal.service.OrganizationLocalService"))
thumbnail
9年前 に Jitendra Rajput によって更新されました。

RE: how to convert long type value to string type in velocity template?

Liferay Master 投稿: 875 参加年月日: 11/01/07 最新の投稿
$getterUtil is used in default init.vm also if every thing is ok then it should work in your case also.
9年前 に R DS によって更新されました。

RE: how to convert long type value to string type in velocity template?

Junior Member 投稿: 41 参加年月日: 13/10/15 最新の投稿
Ok, and do you known how to solve my other problem? How to get static field com.liferay.portal.model.ResourceConstants.SCOPE_INDIVIDUAL in Velocity?
thumbnail
9年前 に Jitendra Rajput によって更新されました。

RE: how to convert long type value to string type in velocity template?

Liferay Master 投稿: 875 参加年月日: 11/01/07 最新の投稿
I never tried to load any static field in velocity template but easy way is the pass directly value of that static field in your case you can directly pass 4
public static final int SCOPE_INDIVIDUAL = 4;
9年前 に R DS によって更新されました。

RE: how to convert long type value to string type in velocity template?

Junior Member 投稿: 41 参加年月日: 13/10/15 最新の投稿
Thank you, but I'm not able to get this damn code working:
#set ( $hasNotPermission = $resourcePermissionService.hasResourcePermission($user.getCompanyId(), "com.liferay.portal.model.Layout", 4, "103742", 1179813, "ACT_ID") )

103742 is a valid layout primary key, 1179813 is a valid role id, ACT_ID is a valid resource permission id.

Addictionally, I've built a servlet doing the same job in Java and this work! emoticon
// Some code to get site layout and user role...
boolean hasPermission = ResourcePermissionLocalServiceUtil.hasResourcePermission(user.getCompanyId(), "com.liferay.portal.model.Layout", ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(layout.getPrimaryKey()), userRolesIds, "ACT_ID");


emoticon
9年前 に R DS によって更新されました。

RE: how to convert long type value to string type in velocity template?

Junior Member 投稿: 41 参加年月日: 13/10/15 最新の投稿
R DS:
Thank you, but I'm not able to get this damn code working:
#set ( $hasNotPermission = $resourcePermissionService.hasResourcePermission($user.getCompanyId(), "com.liferay.portal.model.Layout", 4, "103742", 1179813, "ACT_ID") )

103742 is a valid layout primary key, 1179813 is a valid role id, ACT_ID is a valid resource permission id.

Addictionally, I've built a servlet doing the same job in Java and this work! emoticon
// Some code to get site layout and user role...
boolean hasPermission = ResourcePermissionLocalServiceUtil.hasResourcePermission(user.getCompanyId(), "com.liferay.portal.model.Layout", ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(layout.getPrimaryKey()), userRolesIds, "ACT_ID");


emoticon


UPDATE:
This code works:
#set ($resourcePermissionService = $serviceLocator.findService("com.liferay.portal.service.ResourcePermissionLocalService"))

I was missing Local in ResourcePermissionLocalService.
#set ( $hasNotPermission = $resourcePermissionService.hasResourcePermission($user.getCompanyId(), "com.liferay.portal.model.Layout", 4, "103742", $userRoleId, "ACT_ID") )


But i wont to set resource id parameter (103742) dinamically. So i inject $nav_item.getPrimaryKey() in above code, but this method returns a long, and the method accept String.

So the *last* question is: how can i convert long to String in Velocity?

Thanks
9年前 に R DS によって更新されました。

RE: how to convert long type value to string type in velocity template?

Junior Member 投稿: 41 参加年月日: 13/10/15 最新の投稿
SOLVED.

I convert long to String with a custom velocity service as explained here: https://www.liferay.com/it/web/raymond.auge/blog/-/blogs/custom-velocity-tools-and-liferay-6-0

Note that for Liferay 6.2 the situation is a bit different as explained in Simon Göransson's post:
..To get this to work in Liferay 6.2 you can add this in you web.xml and remove the listener.
<context-param>
<param-name>portalContextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
thumbnail
7年前 に Enrique Valdes Lacasa によって更新されました。

RE: how to convert long type value to string type in velocity template?

Junior Member 投稿: 92 参加年月日: 14/07/29 最新の投稿
I accomplished this by doing:

#set($stringNumber= $longNumber.toString())