Multiple render method in Liferay MVC Portlet

Multiple render in portlet is used to show different views. This is required when different actions requires different results to show.
Many times we need different views to display the output after various action performed. In liferay MVC view.jsp is use to render portlet output.

Let us see by example. Suppose, our portlet shows the list of students in tabular format. It shows Student Name, Age and Standard. Name is a clickable and on clicking it, it will show full details about the students (like his/her address, parents name, contact no etc).


In this case we need to put multiple render logic (one is to show student list and second is to display student details) in one jsp (view.jsp) with if-else condition.


This will not only mass up view.jsp  but creates complexity. If we display multiple render logic in separate jsp, it’s easy to maintain.

In this article we will see step by step, how to achieve multiple render (jsp) in Liferay MVC portlet.


STEP-1: CREATION OF LIFERAY PORTLET


Refer my previous blog on How to create custom Liferay Portlet and create liferay MVC portlet. Give project name as multiple-render and eclipse will append -portlet at the end so that final project name will become multiple-render-portlet.


Create portlet class call called  MultipleRenderPortlet under package com.opensource.techblog.portlet. Also update portlet class entry in portlet.xml file. After this the project structure will look like below screenshot.
Multiple render method in Liferay MVC Portlet - project-structure
You can observe that by default view.jps has been created at root level under /docroot folder.


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


STEP-2: ADD LOGIC TO PORTLET CLASS


Add following code to portlet class


More explanation in Final Flow section of this blog.


STEP-3: Add two JSPs- Multiple render


Add two jsp called studentDetail.jsp and studentList.jsp at root level (under docroot folder). After doing this, the project structure will look like below screenshot.

Multiple render method in Liferay MVC Portlet - adding-two-new-jsp

In our this scenario, First, studentList.jsp will be displayed. Clicking on the link in this JSP, studentDetail.jsp should display. To explain, how to have multiple render (jsp) in Liferay MVC, I have put pseudo code.


In studentList.jsp, we will create one link which will simply show output of studentDetail.jsp.


Let me explain, what is the approach to display multiple render(jsp) in Liferay MVC portlet. Following image shows this approach.

Multiple render method in Liferay MVC Portlet - Approach-of-multiple-render

Approach:-

  • We will have view.jps, which we will keep it blank. View.jsp is the default jsp which will show final output.
  • First, we have to render studentList.jsp. So in render method of Liferay MVC portlet class, we will include it.
  • in studentList.jsp file, we will create link, which simply point to studentDetail.jsp.
  • We will point to respective jsp by its path defined in portlet.xml file


Next, add code to studentList.jsp so that it will look like below snippet

  • We have created action url and passing one parameter called pageName, which we will access it in action method in portlet class.

 STEP-4: ADD Path of studentDetail.jsp and studentList.jsp in portlet.xml file

Next, we will define path of these jsp as initial parameters of portlet in portlet.xml file, so that it will look like below snippet

Explanation:-
  • You can observe that we have added two init param and defined the respective jsp path.
  • These init param we can access in portlet class to get the jsp path.

Final Flow

  • When we place the portlet on page, its render method (defined in portlet class in STEP-2 ) will be called.
  • In this(render) method, we are getting request parameter by calling get method of Liferay util class ParamUtil.
  • get method of ParamUtil will take 3 parameters. First is the PortletRequest from where we will get the parameter, second is the parameter name(renderPage) and third is the default value in case if the parameter will not found in request.
  • First time we will not get renderPage parameter. So it will return studentList-jsp.
  • Then we are calling getInitParameter method and passing the value of renderPage parameter. So first time it will search for init parameter studentList-jsp.
  • We have defined init parameter called studentList-jsp and its value is path of studentList.jsp in portlet.xml file. So first time, it will give path of studentList.jsp
  • The next is, calling method include method and passing the path of studentList.jsp,request and response objects.
  • include method simply include the output of studentList.jsp file into view.jsp file.
  • Since we kept view.jsp as blank, we will get the final output as studentList.jsp file.
  • In studentList.jsp , we created one link and passing action url with one parameter called pageName. Value of this parameter is studentDetail-jsp(which is nothing but the init parameter name which we have defined in portlet.xml file.
  • When we click the link, the process action method will be called. We have defined the name attribute in action url with matching name value of @processAction  annotation in portlet, so it will call process action method called showStudenDetailPage .
  • In this action method, we are getting the request parameter pageName, which we have passed in action url in studentList.jsp file.
  • soon after we are calling actionResponse.setRenderParameter and passing the value of pageName in it.
  • In Portlet, soon after the action method call, portlet container will call render method.That is the reason, we make void return type of process action method.
  • actionResponse.setRenderParameter method will set the parameter in request object, so that it will be available in render method. Note that this is not equivalent of request.setAttribute. In render method we can access this request parameter.
  • When control goes to render method, we will get this render parameter (pageName which is nothing but the init parameter of studentDetail.jsp file) and passing it to getInitParameter method will give the path of studentDetail.jsp file.
  • On clicking include method, the output of studentDetail.jsp will be included in view.jsp file.
  • Since view.jsp is blank, the output will be of studentDetail.jsp file.
So finally it will looks like below screenshot.
 
As soon as we place the portlet on page, output of studetnList.jsp as below screenshot
Multiple render method in Liferay MVC Portlet - studentList.jsp
When we click on the link Go To Student Detail JSP, it will show output of studentDetail.jsp. Since we kept only one line in studentDetail.jsp file, it will show final output as below
Multiple render method in Liferay MVC Portlet - studentDetail.jsp
In short, its only one render method but it will serve multiple JSP files.
 

Summing Up

  • Out of the box, Liferat mvc portlet has only one jsp (view.jsp)
  • It’s easy to maintain multiple render logic if it’s placed in different views.
  • We can point which view we want to render in portlet class.
I would recommend looking at index page ‘A Complete Liferay Guide‘ to browse all topics about liferay.

Download Source code 
Download Source

Share This Post

7 Comments - Write a Comment

  1. Thanks for the post.
    you have not mentioned the way how can we transfer the id that actually the JSP which student detail to display.

    the bottom line please tell us how can the studentDetail.jsp receive an id from studentList.jsp if the list has multiple dynamic data records

    Reply
    1. Hi Hatem,

      Welcome to Tech blog and thanks for appreciation. Answer to your question:-

      From any JSP, when page get submitted, it will reach to ‘render’ method or portlet and from there you can redirect to multiple JSPs. So you can pass id from studentList.jsp as a parameter, once receive in ‘render’ method of portlet, set it to request attribute and then it will be available studentDetail.jsp

      Let me know if this clarify your question. Feel free to ask questions / give suggestions.

      Regards
      Nilang

      Reply
  2. Hello,
    I have a portlet that displays search results in a table (a list of projects). Then when the user clicks on a specific project, I need the project detail information to open up in a separate tab. I have a project search jsp and a detail jsp page that loads (via jquery load) in a separate tab. But how do I pass an attribute to the detail jsp page (like the project id or the whole project model)? Please let me know if there’s a better way to accomplish this behaviour.
    Thanks,
    Ruchi

    Reply
    1. Hi Ruchi,
      First of welcome to Techblog. In this case when you click on perticular link (to open detail jsp) you can store the PK of each record and pass it while triggering the click on that link to open detail.jsp. Let me know if you need any other help.

      Regards
      Nilang

      Reply
    1. Hi Mahesh,

      Thanks for writing and welcome to tech blog. Yes you can have doView() and render() in same portlet. But in real scenario, render() is enough, you no need to have two methods. For sequence, I guess doView() will be called first. However you can put some logger and see which one is called first.

      Regards
      Nilang

      Reply

Post Comment

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