Generic Portlet

Generic Portlet

A generic portlet is purely JSR 286 compliant until and unless if we don’t use any Liferay API or configuration files. A generic portlet can be deployed in any java based portal which supports JSR 286.

To develop generic portlet we generally create our own custom class which generally extends GenericPortlet abstract class.

Step1 : Lets create a custom class which extends GenericPortlet 

Generic Portlet
package com.proliferay.demo;import java.io.IOException;import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
/**
*
* @author Pro Liferay
*
*/
public class DemoGenericPortlet extends GenericPortlet{public void init() {viewTemplate = getInitParameter(“view-template”);
}

public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {include(viewTemplate, renderRequest, renderResponse);
}

 

protected void include(
String path, RenderRequest renderRequest,
RenderResponse renderResponse)
throws IOException, PortletException {PortletRequestDispatcher portletRequestDispatcher =
getPortletContext().getRequestDispatcher(path);

if (portletRequestDispatcher == null) {
_log.error(path + ” is not a valid include”);
}
else {
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}

protected String viewTemplate;

private static Log _log = LogFactoryUtil.getLog(DemoGenericPortlet.class);

}

We will explain each of the method one by one.

public void init()  
 Once we deploy the portlet this method will be called first and it willlookfor view-template parameter in the portlet.xml file. This is calledinitialization parameter. Lets define this parameter in portlet.xml<init-param>
<name>view-template</name>
<value>/jsp/genericdemo/view.jsp</value>
</init-param>

So after deployment of the portlet the value of protected String

viewTemplate would beviewTemplate = “/jsp/genericdemo/view.jsp”;

So we have initialized viewTemplate in init method.

public void doView()

When the portlet will be added to the portal page this method will be

executed.  This is method is called basically for rendering the portlet. You

might be thinking how the JSP page is rendered.  Here are the sequences

1. When you add the portlet in the portal page doView method will be called

2. doView method call  include(viewTemplate, renderRequest, renderResponse);

3. Inside include method

PortletRequestDispatcher portletRequestDispatcher =
getPortletContext().getRequestDispatcher(path);

where the path is jsp path which is nothing but /jsp/genericdemo/view.jsp

Now deploy the portlet ….

Generic Portlet

generic-portlet

 

 
Scroll to Top
%d