Portlet Lifecycle method – render()

portlet lifecycle method render is responsible for generating portlet content. it’s called when portlet is executed in render phase
Portlet lifecycle method render method represents render phase of Portlet. During render phase, portlet generates the content based on its current state(Normal, Minimize, Maximize). Render method /phase of all the portlets on same page is called every time when 

    • that page is refreshed or
    • portlet’s renderURL is called. (triggering render method by Render URL) or
    • any of the portlets on that page completes action or event phase

We will take the same Liferay plugin project (portlet) that we created in previous blog – Portlet Lifecycle method-init(). In the portlet class, we will override render() method which is originally defined in GenericPortlet.

Portlet lifecycle method render

Override render() method in PhaseAndLifecyclePortlet class and put some logs as per below code snippet.


Explanation:-

  • render() method is overridden from GenericPortlet
  • In this method we placed log which will be displayed when render method is called.
  • At the end of this method, we make call of super.render(request,response). it will call GenericPortlet’s render() method to perform some back end processing. (like, send the portlet content to jsp defined in portlet.xml as view-template init parameter).
  • Without making call of super.render(), our overridden render method will not work properly.
  • render() method takes objects of type RenderRequest(interface) and RenderResponse(interface) as parameters. 
  • RenderRequest and RenderResponse are different than ServletRequest and ServletResponse. 
  • Portlet doesn’t have direct access of ServletRequest and ServletResponse.
  • RenderRequest and RenderResponse extends PortletRequest and PortletResponse interface as shown in below screenshot

Lifecycle method render - render lifecycle method of Portlet

Save PhaseAndLifecyclePortlet  class and deploy the portlet. You will see init() method is called during deployment of this portlet because every time we deploy the porltetPortlet container will destroy existing instance of portlet and will create new instance. This time init() method is called.

Portlet lifecycle method render is called in following 3 scenarios.

Scenario-1 render method is called when liferay page is refreshed.

Deploy the portlet and refresh the page on which this portlet is placed (first you have to place the portlet on some page if its not already placed) and logs in server console looks like below screenshot.
render_in_console
  • From the log, you can observe that every time when the page is refreshed, portlet’s render() method is called. 
  • To understand it further, you can develop few more sample portlets and place them all in same page. Put logs in each portlet’s render() methods.
  • Refresh the page and you will observe that all portlet’s render() method is called. However portlet specification doesn’t give any guarantee about the order of render method of portlets on same page. Means which portlet’s render method is called first is unpredictable.
 
Scenario-2 render method is called when renderURL is called.
 

What is render URL ?

Servlets are called by its mapping url (defined in web.xml). Portlet can’t be mapped by direct url like servlet. Portlet container provides certain tags /  util classes which is used by portlet to generate url which points to itself. In other words this url is referring to portlet that creates it. This url is called Portlet URLFollowing are different types of PortletURL.

  • renderURL (will call render method of portlet)
  • actionURL (will call processAction method of portlet)
  • serveResourceURL (will call serveResource method of portlet)

Portlet lifecycle method render is called when renderURL of that portlet is clicked. renderURL points to the portlet for which it is created. It may points to same portlet or other portlet based on parameters are set in render URL. Following are different ways to create renderURL. Detailed explanation is given in respective blog.

 
Scenario-3 render method is called when action method of any portlet on page is completed.

Multiple portlets can be placed on same Liferay page. When any one portlet completes processAction() method then render() method of all portlets on that page will be called. In this case user has not directly interacted with other portlet but still they generates the content. We will see more about this in separate blog.

Summing Up

  • Portlet lifecycle method render represents render phase. In render phase, portlet generates content based on its state
  • Portlet lifecycle method render is called when page is refreshed or one portlet finish action phase on same page or the portlet is called by render URL from same or other portlet.
  • to know about two phases (render and action), you may refer this link.

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

 Download Source

Recommended For You

About the Author: Nilang

Nilang Patel is a technology evangelist who loves to spread knowledge and helping people in all possible ways. He is an author of two technical books - Java 9 Dependency and Spring 5.0 Projects.

2 Comments

    1. Yes absolutely, you can have multiple render for spring mvc portlet but not for Liferay MVC portlet. You can refer my blog on liferay MVC and Spring MVC and their difference (i.e Liferay MVC vs Spring MVC portlet in liferay)

Leave a Reply

Your email address will not be published. Required fields are marked *

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