Fórumok

Invoke a Java class in portal_normal.vm

Sam Wan, módosítva 14 év-val korábban

Invoke a Java class in portal_normal.vm

Liferay Master Bejegyzések: 660 Csatlakozás dátuma: 2009.03.03. Legújabb bejegyzések
Hi,

Can anyone tell me how to invoke a java class in portal_normal.vm or in any *.vm?

eg. in portal_normal.vm, I want to do something like this:



#set ($prodId = $MyProductUtil.getProductFirstId())
#set ($prodDesc = $MyProductUtil.getProductDesc($prodId))

<div><center>
Product ID: $prodId
Product desc: $prodDesc
</center></div>



In order to call MyProductUtil.class, how to configure liferay environment so that the reference of its class is found in portal_normal.vm file?

Thanks
Sam
Mazhar Anwar, módosítva 14 év-val korábban

RE: Invoke a Java class in portal_normal.vm

Regular Member Bejegyzések: 125 Csatlakozás dátuma: 2010.02.05. Legújabb bejegyzések
You can achieve it by extending VelocityVariables.java in portal source.
thumbnail
Shagul Khaja, módosítva 14 év-val korábban

RE: Invoke a Java class in portal_normal.vm

Liferay Master Bejegyzések: 758 Csatlakozás dátuma: 2007.09.27. Legújabb bejegyzések
Hi Sam,

The below links may help.

http://www.liferay.com/community/forums/-/message_boards/message/3376997

http://www.liferay.com/community/wiki/-/wiki/Main/Access+to+Liferay+services+in+Velocity

If the servicelocator does not help, then you could look into the suggestion from Mazhar.
Sam Wan, módosítva 14 év-val korábban

RE: Invoke a Java class in portal_normal.vm

Liferay Master Bejegyzések: 660 Csatlakozás dátuma: 2009.03.03. Legújabb bejegyzések
Shagul Khaja:
Hi Sam,

The below links may help.

http://www.liferay.com/community/forums/-/message_boards/message/3376997

http://www.liferay.com/community/wiki/-/wiki/Main/Access+to+Liferay+services+in+Velocity

If the servicelocator does not help, then you could look into the suggestion from Mazhar.


The second link (ServiceLocator) might be something that I am looking for.
I think I can write something like this:


#set($myServiceUtil = $serviceLocator.findService("au.com.ip6.portal.service.MyServiceUtil"))

Product name: $myServiceUtil.getMyProductName(1)
Product Descriptoin: $myServiceUtil.getMyProductDesc(1)



Thanks very much for the reference.
I will try it out see if it works.

Thanks
Sam
Vandana Chirame, módosítva 9 év-val korábban

RE: Invoke a Java class in portal_normal.vm

New Member Bejegyzések: 8 Csatlakozás dátuma: 2014.11.27. Legújabb bejegyzések
Can you please tell me

1. where to put java file in liferay theme and
2. how to use java methods in .vm file?

Thanks in advance.
Vishal Patel, módosítva 9 év-val korábban

RE: Invoke a Java class in portal_normal.vm

Junior Member Bejegyzések: 83 Csatlakozás dátuma: 2014.11.24. Legújabb bejegyzések
Hi Vandana,

First of all there is no need to put any java file into themes.

To user any liferay services ,utilities OR custom services & utilities, Liferay provide $serviceLocator Util.

What $serviceLocator does?

It will search your service name into Liferay bean definitions & will try to locate defined bean for service which you have opted.

Like,

$userLocalUtil = $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))

So this will find UserLocalServiceUtil & then you can use $userLocalUtil in theme like, $userLocalUtil .getUser(userId).

The same thing is happen with custom class. But remember your custom class (Services & Util ) must be register with Liferay Beans definitions.Otherwise, It will throw error like "No Such bean defination found".

In Theme ,

#set($myProductServiceUtil = $serviceLocator.findService("com.liferay.example.MyProductService"))
#set ($prodId = $myProductServiceUtil .getProductFirstId())





Hope you understand it.
Vandana Chirame, módosítva 9 év-val korábban

RE: Invoke a Java class in portal_normal.vm

New Member Bejegyzések: 8 Csatlakozás dátuma: 2014.11.27. Legújabb bejegyzések
Can you please tell me in which file we can register Liferay Beans definitions.
Mohamed Faisal, módosítva 9 év-val korábban

RE: Invoke a Java class in portal_normal.vm

Junior Member Bejegyzések: 90 Csatlakozás dátuma: 2009.08.12. Legújabb bejegyzések
Please refer below link
http://www.opensourceforlife.com/2012/06/custom-velocity-variable-in-liferay-61.html

Regards,
Faisal
thumbnail
Jitendra Rajput, módosítva 9 év-val korábban

RE: Invoke a Java class in portal_normal.vm

Liferay Master Bejegyzések: 875 Csatlakozás dátuma: 2011.01.07. Legújabb bejegyzések
You can add required logic in your custom portlets service class and access the same in velocity file.

Refer to below example for more information.

http://itsliferay.blogspot.in/2012/04/access-custom-portlet-service-in.html
Vandana Chirame, módosítva 9 év-val korábban

RE: Invoke a Java class in portal_normal.vm

New Member Bejegyzések: 8 Csatlakozás dátuma: 2014.11.27. Legújabb bejegyzések
Thanks all...

@jitendra.. for your way also i need to make hook project?
thumbnail
Jitendra Rajput, módosítva 9 év-val korábban

RE: Invoke a Java class in portal_normal.vm

Liferay Master Bejegyzések: 875 Csatlakozás dátuma: 2011.01.07. Legújabb bejegyzések
Vandana Chirame:


@jitendra.. for your way also i need to make hook project?


Without creating hook also it will work. Move your portlet services to lib/ext and override property using portal-ext file.

If services are not associated with any portlet then you can create hook and inside that you can create services.

Thank you,
Jitendra
Vandana Chirame, módosítva 9 év-val korábban

RE: Invoke a Java class in portal_normal.vm

New Member Bejegyzések: 8 Csatlakozás dátuma: 2014.11.27. Legújabb bejegyzések
Thank you so much emoticon
Vandana Chirame, módosítva 9 év-val korábban

RE: Invoke a Java class in portal_normal.vm

New Member Bejegyzések: 8 Csatlakozás dátuma: 2014.11.27. Legújabb bejegyzések
But when using hook project run method of hook project continuously printing my sysouts... means run method calling frequentlly???. Is this effects performance??