What is Portlet namespace

Portlet namespace is used to uniquely identify each portlet. It’s unique identifier associated to each portlet by portal.

portlet namespace

What is portlet namespace ?

Portlet namespace is a unique value associated to each portlet by portal. In other word, each portlet will have it’s own unique portlet namespace. You no need to worry about how it’s generated. Liferay portal will generate it for you.

Why portlet namespace is required ?

Consider a scenario where you are creating input element called ‘name‘ in view.jsp for given portlet. At the time portlet development, you don’t an idea what other portlets are placed on same page along with your portlet.

Assume that some other portlet which is placed on same page also has one html element called ‘name‘. In this scenario there will be a name collision when you try to access this element(name) through javascript.

Or

Assume that you gave id=”test1″ to some html element for your portlet. There are high chances that some other portlet has some other html element with same id(test1). In this case if you try to access id=”test1″ through Javascript then again it’s name collision.

To avoid this, portlet namespace should be used. Since portlet namespace is unique for each portlet, if you append it along with name of html attribute, then there won’t be any chance of name collision (especially when trying to access html element through java script).

How to get portlet namespace value ?

There are following 2 ways to get portlet namespace

  • Using <portlet:namespace/> tag
  • Using PortletResponse.getNamespace() method.

Using <portlet:namespace/> tag

Portlet default implementation (available in portlet.jar @ tomcat-bundle/lib/ext folder) provides certain tags. ‘portlet‘ is one of such tag. You can get current portlet’s namespace by <portlet:namespace/> tag. To avail this tag you must have to include following taglib definition in JSP

You can prepend portlet namespace through this tag to any html element name to make it unique so that there won’t be any name collision.

for example, portlet namespace can be used like below code snippet

Explanation

  • I have just append <portlet:namespace/> for name attribute in JSP. When this JSP renders, above code will look like below snippet

  •  
  • _requirednamespace_WAR_requirednamespaceportlet_ is name space for portlet  required-namespace-portlet. this namespace is generated by <portlet:namespace/> tag.
  • Since each portlet has it’s own unique namespace, it will help to avoid name collision. Just like name in above example, you can prepand portlet namespace to other html attributes like id.
  • This portlet is non-instantiable. If it’s instantiable then liferay portal generates 4 extra alpha-numeric instance Id in above namespace. for example, if this portlet is instantiable then it’s namespace would be _requirednamespace_WAR_requirednamespaceportlet_abcd.
  • These last 4 alpha-numeric instance Id is unique per that portlet’s instance. There can be more than one instance for instantiable portlet and liferay portal will generate unique instance Id for each instance.

Using PortletResponse.getNamespace() method.

Liferay provide certain implicit objects. <portlet:defineObject /> tag is used to make these implicit objects available in JSP. Second way to get portlet name space is to call PortletResponse.getNamespace() method in scriptlet in JSP. for example

You can set it in some variable and refer it across the JSP page.

RenderResponse, actionResponse and ResoruceRequest are  subclass of PortletResponse. You can get portlet namespace by calling getNamespace() method on respective implicit objects of above classes as below.

  • renderResponse.getNamespace()
  • actionResponse.getNamespace()
  • resourceRquest.getNamespace()

Below are few example how to use portletResponse.getNamespace() to get portlet namespace

there is no difference between getting namespace from above two methods. However the first approach (<portlet:namespace /> tag) is more preferable way.

There are some situation in which you have to use second approach (portletResponse.getNamespace()) to get portlet namespace.

For example you are writing your own tag or if you are using some other tag and you need to pass portlet namespace in attribute of this tag then resolving tag inside another tag is not possible but you can still use portletResponse.getNamespace() in scriptle (if that attribute allow to accept scriptlet value) to pass portlet namespace in that tag.

Summing Up

  • Portlet namespace is unique identifier generated by portal for each portlet.
  • Portlet namespace is helpful to avoid name collision while accessing html elements in javascript.
  • <portlet:namespace/> tag is used to get portlet name space in JSP
  • portletResponse.getNamespace() is another way to get portlet namespace in scriptlet.
  • You can refer liferay forum link to get more information about portlet namespace.

Share This Post

Post Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.