Portlet namespace is used to uniquely identify each portlet. It’s unique identifier associated to each portlet by portal.
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.
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
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
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
<input type="text" name="<portlet:namespace/>param1">
- I have just append <portlet:namespace/> for name attribute in JSP. When this JSP renders, above code will look like below snippet
- XHTML1<input type="text" name="_requirednamespace_WAR_requirednamespaceportlet_param1">
- _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.
Below are few example how to use portletResponse.getNamespace() to get portlet namespace
<input type="text" name="<%=renderResponse.getNamespace()%>StudentName"
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.
- Portlet namespace is unique identifier generated by portal for each portlet.
- <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.