create render URL By Java API in Portlet class and JSP

Liferay provides PortletURLFactoryUtil class to create render URL in portlet. such render URL can point to other portelt thay may be placed on different page.

Render URL can be created with Java API. Situations arises when you need to create render URL in portlet. For example you want to make a call to portlet B from portlet A. In this case render URL of portlet B should be created in Portlet A.

If you create render URL by portlet tag in portlet A, then it will point to same portlet – portlet A. In case, if you want to create render url which points to portlet B, you need to create render URL with Java API.

Create LiferayMVC portlet with plugin project name as renderURL-by-Java-api and -portlet will be appended by Liferay IDE. I gave portlet class as com.opensource.techblog.portlet.RenderURLByJavaAPIPortl. Project structure should look like below screenshot.
Project structure to create render url in Java API

There are two places where you can create portlet URL with Java API. 

Creating render URL by PortletURLFactoryUtil in Portlet class.

PortletURLFactoryUtil is a util class provided by Liferay to create render URL. You can create renderURL for current as well as other portlet by PortletURLFactoryUtil class.

Add following code in render() method



Explanation:-

  • First few lines in render() method are about reading request parameter and displaying it in log.
  • After that we are getting ThemeDisplay object from request attribute.
  • In next step, we called PortletURLFactoryUtil .create method. This method takes following parameters
    • PortletRequest :– RenderRequest is extending PortletRequest so we are passing RenderRequest object.
    • PortletName (String):- Name of the portlet for which this RenderURL is pointing to. we assign themeDisplay.getPortletDisplay().getId().
      • themeDisplay.getPortletDisplay() returns object of type PortletDisplay which represents Portlet content.
      • getId() method of PortletDisplay returns portlet id which is nothing but p_p_id.
      • If we want to call other portlet’s render method, we have to pass that portlet’s Id in this parameter.
    • plid (long):- It represent page layout id. (PK for Layout table). 
      • Layout in liferay represent liferay page and its information is stored in Layout table. 
      • plid represents layout id of liferay page on which the target portlet is placed and we want to call its render method by this renderURL. 
      • we used themeDisplay.getPlid() because we want to make call of render URL of same portlet. themeDisplay.getPlid() returns plid of current page(layout).
    • lifecycle(String):- We need to pass lifecycle (phase of the portlet). 
      • Here we gave PortletRequest.RENDER_PHASE
      • PortletRequest is interface provided by default implementation (from portlet.jar resides in tomcat/lib/ext path).
      • Base on this value, type of generated Portlet URL will be decided.
        • If PortletRequest.RENDER_PHASE is passed,then this Portlet URL is work as render URL.
        • If PortletRequest.ACTION_PHASE is passed, then this Portlet URL is work as action URL
        • If PortletRequest.RESOURCE_PHASE is passed,then this Portlet URL is work as resource URL
  • PortletURLFactoryUtil .create method returns object of type PortletURL.
  • We can pass additional parameter by calling setParameter method on PortletURL. In our case, param parameter is passed with value “This parameter comes from Render URL generated with Java API”
  • We can set portlet mode and window state on PortletURL by setPortletMode and setWindowState methods respectively.
  • At the end, we are storing portlet URL in request scope.
In JSP, add following code to show this portletURL as link

 

Explanation:-

  • We created new link (anchor tag) and set its href to portlet URL (that we stored in request scope in render method) through EL.
Save portlet class and view.jsp. Deploy the portlet and you will see above link. By clicking this link, the parameter(param) which we set in render URL is displaying in logger.

creating render URL with JAVA API in jsp. 

JSP provides certain objects ready to use like request, response, config, out, session etc. They all are called JSP implicit objects. Portal specification also provides certain ready to use objects in JSP. They are called Portlet implicit objects.
Portlet default implementation provides certain implicit objects. However they are not directly accessed just like JSP implicit objects. We need to add following tag library in jsp

Explanation:-

  • <portlet:defineObjects> tag will make portlet implicit objects available in JSP. 
  • This implicit objects includes renderRequest, renderResponse, actionRequst, actionResponse, eventRequest, eventResponse, resourceRequest, resourceResponse, portletConfig, portletName etc.
On top of these implicit object, liferay also provide additional implicit objects. These implicit objects are liferay specific only so they will not be available on other portal container.
 
To avail liferay implicit objects, we need to add following tag library in JSP

Explanation:-
  • <liferay-theme:defineObjects/> tag will make all liferay specific implicit objects in JSP.
  • These implicit objects includes themeDisplay, company, user, plid, layout, local, theme, permissionChecker etc.
We can create renderURL directly in JSP with liferay specific implicit objects. Let’s see how to do that. Add following code in JSP

Explanation:-

  • renderResponse implicit object is used to create PortletURL. In our case we want to create render URL so we called method createRenderURL()
  • Once render URL is created, we can set additional parameter, window state and portlet mode similar way a render URL created with PortletURLFactoryUtil class.
  • At the end, we created one more link and set its href to this render URL.
Save view.jsp and deploy the portlet. Refresh the page and click on this link. You will observe that, the parameter which we set here will be printed in server console from render method().

Summing Up

  • PortletURLFactoryUtil class is used to create render URL in portlet class. Such render url can point to different portlet which is placed on same or different page.
  • renderResponse implicit object is used to create PortletURL in JSP.
  • We can set portlet mode and window state while creating renderURL in Java API.
  • We can pass additional parameter by calling setParameter on object of PortletURL
  • renderURL created with Java API in Portlet class or JSP can be used to call other portlet on same/other liferay page. For this we have to give portlet name and plid of target portlet.
  • You may like to get more information about render phase from this link

Following are other possible ways to create render URL in Liferay

Share This Post

4 Comments - Write a Comment

    1. Hi Krishna,

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

      Our custom portlet extends LiferayMVC portlet. this LiferayMVC extends LiferayPortlets and LiferayPortlet extends GenericPortlet. GenericPortlet do many back end job so that we can focus only business logic. You can refer my blog :- http://www.opensource-techblog.com/2012/08/creating-custom-liferay-mvc-portlet.html to understand the portlet hierarchy.

      When we make call super.render(), it will ultimately call its GenericPortlet’s render method which will take the path of view.jsp from portlet.xml file and forward the control to it.

      If you are familiar with Servler, generally, If we want servlet,,we are extending HTTPServlet. This HTTPServlet is doing back end job like, mapping the request to particular method (Get or Post) from service method. The only thing we have to do is to write business logic to either doGet or doPost method.

      Similar concept works in case of Portlet. Let me know if this solve your doubts.

      Feel free to ask questions / give suggestion and join this blog socially to get updated.

      Regards
      Nilang

      Reply

Post Comment

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