Portlet Lifecycle method – init()

Initialization phase of Portlet is represented by init() method. When portlet is deployed, portlet container will destroy any existing instance and create new instance of portlet. At this time, it will call init() method.

Like Servlet, Portlet can initialize any back end resource or to do any one time activity in init() method. In short init() method is place for initialization.

 
Servlet reads init parameter from web.xml in servlet’s init() method. Similarly Portlet reads init parameters from portlet.xml in init() method. Generally these init parameters are used to define page flow of the portlet.
We will see how lifecylce methods works by creating Liferay MVC portlet. Refer my blog on how to create Liferay MVC portlet. Give project name as portlet-phase-lifecycle and eclipse will append -portlet at the end. 

I gave portlet class as com.opensource.techblog.portlet.PhaseAndLifecyclePortlet. The project structure looks like below screenshot.
Portlet Lifecycle method init  - Project structure

As mentioned in Liferay MVC Portlet blog, 

  • GenericPortlet implements Portlet interface.
  • LiferayPortlet extends GenericPortlet and provides additional methods.
  • MVCPortlet is defined by Liferay. It extends LiferayPortlet and provides MVC architecture by providing more additional methods.
  • PhaseAndLifecyclePortlet (our custom portlet) extends MVCPorrtlet.

Lifecycle method init

To understand how lifecycle method init works, we will override it in our custom portlet. There are two versions of init() methods available in Liferay portlet class hierarchy.

init() method available in MVCPortlet

  • Signature:- public void init() throws PortletException
    • This method does not have any parameter. 

init() method available in GenericPortlet

  • Signature :- public void init(PortletConfig config) throws PortletException
    • This method take PortletConfig as a parameter. 
    • PortletConfig object is used to read portlet configuration (defined in portlet.xml). 
    • One of the common use of PortletConig object is to read initialization parameter defined in portlet.xml.
    • PortletConfig is similar to ServletConfig (which is used to read servlet configuration from web.xml).


After Overriding these init methods, our portlet class looks like below code snipped.

Explanation:-

  • LogFactoryUtil is a util class provided by liferay to get log for current class.
  • We have overridden both version of init() method and added some logs. These log statements will be displayed in server console when this portlet is deployed.
  • In second version of init() method, we are reading initialization parameter called ‘view-template’ and putting into log. ‘view-template’ init parameter is defined in portlet.xml file and used to define the path of jsp which renders portlet’s output.

After doing this change, deploy the porltet. Make sure server is up and running. During the Portlet deployment, portlet container will call init() method and you will see below logs in server console.

Portlet Lifecycle method init - Porttlet modes and lifecycles
  • As you can see during the deployment of this portlet, init() method overridden from GenericPortlet is called first followed by init() method overridden from MVCPortlet.
  • This is because GenericPortlet is located at higher position than LiferayMVCPortlet in class hierarchy.
  • Logs also shows the initialization parameter’s(view-template) value (/view.jsp) which is defined in portlet.xml file. We can read all initialization parameter in init() method.

Place this portlet on some liferay page. On refreshing this page, init() methods is not called again. It is only called when portlet is instantiate by portlet container (during deployment on the server). Means every time when we deploy the portlet, it’s init() method is called

Summing Up

  • Portlet lifecycle method init will be called by portlet container during initialization of portlet
  • Its called just once during deployment of portlet.
  • It’s a place where we can define any one time initialization.
Download Source

I would recommend looking at index page ‘A Complete Liferay Guide‘ to browse all topics about liferay.

 

Share This Post

2 Comments - Write a Comment

  1. hi, I’ve try to run this example (using maven) so I have a different structure of the portlet;
    I have seen that I can deploy the portlet on a page; but i do not see the log in any part either in the catalina.out log either in liferay-portal-6.2-ce-ga2/logs.

    What I’m wrong??

    Reply

Post Comment

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