Total Pageviews

Sunday, September 4, 2016

Liferay: A note on Portlet Namespace

Portlet namespace is a unique ID associated to each instance of the portlet provided by the portlet container.

When a team is working on a Liferay project, there are several portlets are created. From my experience, I have seen projects having 150+ portlets. So it’s very difficult to track the client side elements, such as JavaScript function, HTML elements IDs etc., which are being written by several developers. Portlet Namespace is a solution of the same. This tag returns the ID of the portlets which can prefixed with client side elements. This tag returns the ID of the portlet, with instance ID in case of instantiable portlet. Portlet Namespace can be used by several ways, to day we’ll see getting it by tag and PortletResponse object.

Non Instantiable portlet’s ID:
_{Portlet WAR ID}_WAR_{Portlet ID}_

Instantiable portlet
_{Portlet WAR ID}_WAR_{Portlet ID}_INSTANCE_{random ID generated by Liferay}_

Usage of <portlet:namespace />
<portlet:namespace /> gets converted to ID at the time of JSP’s conversion to class.

In JavaScript

So internally the mark-up generated will be as follows:

Usage by PortletResponse

Namespace can be obtained by any of the PortletResponse objects:

  1. renderResponse.getNamespace()
  2. actionResponse.getNamespace()
  3. resourceResponse.getNamespace()

for example, I’ll consider renderResponse object is available on the JSP

To conclude, both the approached are same however <portlet:namespace /> most preferred way. However second one is needed when you are in controller class, utility methods or TLDs.

That’s all for today. Thanks for reading and have a nice day.