Thursday, October 6, 2016

Instanceable vs Non- Instanceable portlet in Liferay


Today we will discuss the difference between Instanceable vs Non- Instanceable portlet in Liferay. 

Instanceable :- If Portlet is instanceable then we can add the portlet more than once on same page.

Non-Instanceable :-  If Portlet is non-instanceable then we can add the portlet only once on a page.

So lets discuss this in detail step by step:-


Step 1:-Create Liferay Project and Portlet 
Create a Liferay plugin project and than create a portlet in it. You can check snapshot in my previous blog Developing a MVCPortlet with Multiple Actions in Liferay.


Step 2:-Change view.jsp
Open view.jsp and paste this content:-

view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
<portlet:actionURL var="testURL" name="test" />
<form action="<%=testURL%>" method="POST">
Name :-<input type="text" name='<portlet:namespace/>name'>
<input type="submit" value="SUBMIT">
</form>
view raw view.jsp hosted with ❤ by GitHub

Explanation:-
Here we simply create a actionURL that is handle by test method in Controller.




Step 3:-Change the Controller
Open your java class and paste this:-

Demo.java
package com.demo;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class Demo extends MVCPortlet {
public void test(ActionRequest request,ActionResponse response) throws IOException, PortletException {
String name = ParamUtil.getString(request, "name");
System.out.println("Name=>"+name);
}
}
view raw Demo.java hosted with ❤ by GitHub


Step 4:-Check the Output for  Non- Instanceable
Deploy the portlet and add this to a page and click on Gear icon and then click on Look and Feel:-



Go to Advanced Styling here you find the portletId.



Portlet ID: #portlet_demo_WAR_instanciablevsnoninstanciableportlet

demo:- This is portlet name mention in portlet.xml
instanciablevsnoninstanciable :- This is Project name 

You can also get the portletId by providing some value in name field and click submit:-


Now check the URL:-

http://localhost:8080/web/guest/welcome?p_auth=VTrkoIh1&p_p_id=demo_WAR_instanciablevsnoninstanciableportlet&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&p_p_col_id=column-1&_demo_WAR_instanciablevsnoninstanciableportlet_javax.portlet.action=test

p_p_id=demo_WAR_instanciablevsnoninstanciableportlet

Here p_p_id is portletId.





Step 5:-Change to instanciable
Open liferay-portlet.xml and after icon tag provide
<instanceable>true</instanceable> 

liferay-portlet.xml
<?xml version="1.0"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.2.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_6_2_0.dtd">
<liferay-portlet-app>
<portlet>
<portlet-name>demo</portlet-name>
<icon>/icon.png</icon>
<instanceable>true</instanceable>
<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>/js/main.js</footer-portlet-javascript>
<css-class-wrapper>demo-portlet</css-class-wrapper>
</portlet>
<role-mapper>
<role-name>administrator</role-name>
<role-link>Administrator</role-link>
</role-mapper>
<role-mapper>
<role-name>guest</role-name>
<role-link>Guest</role-link>
</role-mapper>
<role-mapper>
<role-name>power-user</role-name>
<role-link>Power User</role-link>
</role-mapper>
<role-mapper>
<role-name>user</role-name>
<role-link>User</role-link>
</role-mapper>
</liferay-portlet-app>

Now again deploy the portlet and add the portlet multiple times on page.


Step 5:-Check the Output for Instanceable
Now Go to Gear icon of First portlet and check the portletId.

Portlet ID: #portlet_demo_WAR_instanciablevsnoninstanciableportlet_INSTANCE_reuQSbwUdUnE

Now Go to Gear icon of Second portlet and check the portletId.

Portlet ID: #portlet_demo_WAR_instanciablevsnoninstanciableportlet_INSTANCE_RR0shoUOEuvI

Here you can see INSTANCE id also which is different for both the instances.

Instance Id(First Portlet)  = reuQSbwUdUnE
Instance Id(Second Portlet)  = RR0shoUOEuvI



Conclusion

So if a portlet is non-instanciable than this can be added only once and not contain any instanceId but if portlet is instanceable than we can add this multiple time but each instance contain different Instance Id .

Project Structure:-





Hope this will Help....

Related Post:-


No comments:

Post a Comment

Total Pageviews

1039142

Number Of Unique Visitor

Free counters!