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 a renderURL that 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

//define log for this class
private static final Log _log = LogFactoryUtil.getLog(RenderURLByJavaAPIPortlet.class.getName());

@Override
public void render(RenderRequest request, RenderResponse response)
  throws PortletException, IOException {
   _log.info(" This is render method of RenderURLByJavaAPIPortlet");
   String data = request.getParameter("param");
   String data1= ParamUtil.getString(request, "param","");
   _log.info("parameter with request.getParameter is =>"+data);
   _log.info("parameter with ParamUtil.getString is =>"+data1);
   
   ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
   PortletURL renderUrl =  PortletURLFactoryUtil.create(request, themeDisplay.getPortletDisplay().getId(), themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
   renderUrl.setWindowState(LiferayWindowState.NORMAL);
   renderUrl.setPortletMode(LiferayPortletMode.VIEW);
   renderUrl.setParameter("param", "This parameter comes from Render URL generated with Java API");
   request.setAttribute("renderUrlByJavaAPI", renderUrl.toString());
   
 super.render(request, response);
 }

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 the request attribute.
  • In next step, we called PortletURLFactoryUtil .create method. This method takes the 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 another portlet’s render method, we have to pass that portlet’s Id in this parameter.
    • plid (long):- It represents page layout id. (PK for Layout table). 
      • Layout in Liferay represents the Liferay page and its information is stored in the Layout table. 
      • plid represents the layout id of the 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 works as renderURL.
        • If PortletRequest.ACTION_PHASE is passed, then this Portlet URL is work as an actionURL
        • 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 the 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.
  • In the end, we are storing the portlet URL in the request scope.
In JSP, add the following code to show this portletURL as a link

<a href="${renderUrlByJavaAPI}">Render Url created by Java API</a>
 Explanation:-
  • We created a 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 the 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.
The portlet default implementation provides certain implicit objects. However, they are not directly accessed just like JSP implicit objects. We need to add the following tag library in jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />

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 objects, Liferay also provides 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 the following tag library in JSP
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<liferay-theme:defineObjects/>
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
<%
 PortletURL renderUrlFromJSP = renderResponse.createRenderURL();
 renderUrlFromJSP.setParameter("param1", "This portletULR is created with API in JSP");
 renderUrlFromJSP.setWindowState(LiferayWindowState.NORMAL);
 renderUrlFromJSP.setPortletMode(LiferayPortletMode.VIEW);

%>
<a href="<%=renderUrlFromJSP%>">Render Url created by JAVA API in JSP</a>

Explanation:-

  • renderResponse implicit objects 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.
  • In 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 an additional parameter by calling setParameter on the object of PortletURL
  • renderURL created with Java API in Portlet class or JSP can be used to call other portlets on same/other liferay page. For this, we have to give the portlet name and plid of the target portlet.
  • You may like to get more information about the render phase from this link

Following are other possible ways to create render URL in Liferay

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.

4 Comments to “create render URL By Java API in Portlet class and JSP”

    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 :- https://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

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.