Portlet Action Phase / Lifecycle

portlet action phase is executed in portlet’s action method. It’s called by action URL. During action phase, user will interact with portlet.
Portlet action phase is used to perform some action on portlet. Only one portlet on given page can execute action phase at a time. For example if there are 10 portlets on a page, then action phase can be invoked for one portlet at a time. In action phase portlet can change its status like changing portlet preference or perform CRUD operations.

Portlet specification(JSR-168 & 286) has defined render phase. Question may arise in mind that what is the need of introducing portlet action phase ? Let’s understand this by example.
Before further explanation, let me recall that portlets are designed to generate aggregate response. In other words, more than on portlet on a given page can generate whole page content. (Each portlet will generate small chunk of whole page). Portlet render phase is called when page is refreshed or renderURL of any portlet on that page is clicked.
Now take an example of following scenario.
  • Two portlets (Portlet1 and Portlet2) are placed on same page. 
  • Portlet1 is performing CRUD operation. Assume that there is no action phase so CRUD operation is placed in render method for portlet1.
  • When user clicks on renderURL of portlet1, CRUD operation will be performed in render() method of portlet1.

Issue occurs when use clicks renderURL for portlet2. Let me explain how ?

  • If render() method of any portlet on given page is called then render method of all portlet on that page will be called.
  • When user clicks on renderURL for portlet2 then render() method of portlet1 will also called and CRUD operation is performed unnecessarily.
  • User didn’t interact with portlet1 directly but still CRUD operation on Portlet 1 is performed which must be avoided.
  • Why this happened ? because Portlet doesn’t know what other portlets are placed on the page during development.
Portlet specification introduced portlet action phase to avoid this limitation. Portlet action phase can be called by actionUR and it’s invoked for one portlet at a time on given page. Portlet render phase is called soon after portlet action phase completes.  

Ideally any operation which should not be repeated must be defined in action phase. In other words action phase is best place to write any code that should not be repeated unintentionally(for example CRUD operation we saw above).

Portlet action phase 

  • Portlet’s action method executes portlet action phase.
  • Following sequence diagram shows how action and render phase / life-cycle works for portlet.
Portlet Action Phase sequence

============= Portlet Render Phase ===========================

  • Step-1 : User make request for Page1 where Portlet-A and Portlet-B are placed.
  • Step-2 : Liferay will take that request and pass to portlet container
  • Step-3 : Portlet Container will call render method for Portlet-A
  • Step-4 : Portlet Container will get response from Portlet-A
  • Step-5 : Portlet Container will call render method for Portlet-B
  • Step-6 : Portlet Container will get response from Portlet-B
  • Step-7 : Portlet Container will aggregate response from Portlat-A and Portlet-B.
  • Step-8 : Liferay will send back the response

============= Portlet Action phase followed by Render phase =========

  • Step-9 : Now User send request on Portlat-A to perform CRUD operation.
  • Step-10 : Liferay will call Portlet Action phase
  • Step-11 : Portlet Container will call Action method on Portlet-A.
  • Step-12 : As per Portlet specification, render phase is called when portlet action phase is completed. In this step, portlet container will call render method of Portlet-A.
  • Step-13 : Portlet Container will get response from Portlet-A.
  • Step-14 : As per Portlet Specification, when render method is called for any portlet on page then render() method of all portlet on that page will also get called. In this step portlet container calls render method of Portlet-B.
  • Step-15 : Portlet Container will get response from Portlet-B.
  • Step-16 : Portlet Container will aggregate the response from Portlet-A and Portlet-B and send back.
  • Step-17 : final response will be given back to user.

action URL is used to call action method. You can refer following blogs to know how to create action URL and complete flow.

You can refer my blog on how action method internally invoked for Liferay Portlet to understand how Liferay portal handles action method invocation.

Summing Up

  • Portlet’s action method executes portlet action phase.
  • Any logic or code that should not be repeated must be executed in action phase.
  • Once portlet action phase is completed, portlet container will call render method of same portlet.
  • action URL is used to call portlet action method.
  • You may refer this link to know more about render and portlet action phase.

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

Share This Post

Post Comment

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