Liferay: Add methods/constants to Velocity context

In your Velocity templates you may need to access some parts of your API / constants. Here is one simple way to do this (there are other ways).

Here are the steps:

  1. Create a class that extends com.liferay.portal.kernel.events.Action
  2. Register this action in portal-hook.properties
  3. Enjoy!

1. Create a class that extends com.liferay.portal.kernel.events.Action

This class should be put in a Liferay hook. This Action class will be executed by Liferay once your server has started.

In the below code snippet we add to velocity context:
– one Java object (use a singleton if it’s a util class) : instance of MyUtilClass
– one Java const called “MY_CONST” with the value “WORLD”

@Override
public void run(final HttpServletRequest request, final HttpServletResponse response) throws ActionException {

	Map <String, Object> vmVariables = (Map <String, Object> ) request.getAttribute(WebKeys.VM_VARIABLES);

	if (vmVariables == null) {
		vmVariables = new HashMap < String, Object > ();
		vmVariables.put(“myUtilClass”, getMyUtilClassWrapper());
		vmVariables.put(“MY_CONST”, “WORLD”);
	}
	
	request.setAttribute(WebKeys.VM_VARIABLES, vmVariables);
}

private static final MyUtilClass MY_UTIL_CLASS;

private static MyUtilClassWrapper getMyUtilClassWrapper() {
	if (MY_UTIL_CLASS == null) {
	  MY_UTIL_CLASS = new MyUtilClass();
	}
	return MY_UTIL_CLASS;
}

2. Register this action in portal-hook.properties

servlet.service.events.pre=com.test.MyUtilClassWrapper

Then build & deploy your hook.

Liferay will execute this action in the class MainServlet, check Liferay source code if you want to have a better understanding of the logic.

3. Enjoy!

Now in your velocity templates you can use your Java object and your Java constant:

$myUtilClass.sayHello()
$MY_CONST

There are other ways to do it, I think that this one is the most simple one.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s