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 complete action or event phase

We will take the same Liferay plugin project (portlet) that we created in the 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.

@Override
 public void render(RenderRequest request, RenderResponse response)
   throws PortletException, IOException {
  _log.info(" This is render method of PhaseAndLifecyclePortlet");
  super.render(request, response);
 }

Explanation:-

  • render() method is overridden from GenericPortlet
  • In this method, we placed a log that will be displayed when the render method is called.
  • At the end of this method, we make the 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 a 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 the same page. This 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 their mapping URL (defined in web.xml). Portlet can’t be mapped by direct URL like a 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 the 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 the same Liferay page. When anyone’s 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 portlets but still, they generate the content. We will see more about this in a separate blog.

Summing Up

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

I would recommend looking at the 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 to “Portlet Lifecycle method – render()”

    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.