简体   繁体   中英

Calling Java methods in portal_normal.vm

I have a theme realized in Liferay. I have a form portlet on the control panel that the user can fill. Depending on the values inserted in the portlet the theme homepage should appear differently.

I was thinking to use something like that but it doesn't work:

#set($typeLong = $portal.getClass().forName('java.lang.Long').getField('TYPE').staticValue)
#set($personalOptions = $portal.getClass().forName("com.mypackage.utils.Configuration").getMethod("getStrings", $typeLong).invoke(null,$scopeGroupId))

where my "getStrings" is a method defined in the "Configuration" class with input a long and a String[] array return.

Any advices?

Don't go along that route. It might work in a few circumstances but it's nothing more than a crazy hack. It will break in some kinds of conditions.

Keep in mind that your theme might be in a different classloader than your portlet - thus your com.mypackage.utils.Configuration class might not even be available.

IMHO showing personal content is a piece of business logic that's best encapsulated in a portlet - and you can embed portlets in a theme so that they're automatically shown on each page. The portlet can also be implemented to not show anything if it doesn't need to. Add fancy CSS to your theme (for exactly this portlet) to make it appear special. For example like Liferay's dockbar or chat portlet: Both are portlets with fancy CSS: They don't look like portlets, but indeed they are. And they're automatically embedded in every page.

Separate concerns, have actual content and personal data in portlets. Your future maintainers will thank you (even if it's only the future you who will thank you)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM