create resource URL programmatically in Portlet class and JSP

Liferay provides PortletURLFactoryUtil class to create resource URL programmatically (Java API). You can call serveResource method by such resource URL of same portlet or different portlet kept on different page

Liferay provides facility to create resource URL programatically through Java API. This is useful when you want to make a call to serveResource method of portlet B from portlet A or even same portlet.

Resource url can be created in JSP by portlet tag. Such resource url can point to serveResource method (or serve resource phase) of same portlet only.

In case if you want to call serveResource method of different portlet, you need to create resource url either through liferay-portlet:resourceURL tag or programmatically by Java api.

In this article I will be showing you how to create resource URL programatically through Java API.

resource URL programmatically

 

You need to create LiferayMVC portlet. To make it simple, I have created the MVC portlet with Maven. Give portlet class as com.opensource.techblog.portlet.ResourceURLByJavaAPIPortlet. The project structure would look like below screenshot.

resource URL programmatically - project structure

create resource URL programmatically.

There are two places where you can create resource URL programmatically with Java API

Place 1: resource URL by PortletURLFactoryUtil in Portlet class

PortletURLFactoryUtil class (provided by Liferay) is used to create various portlet URLs (like render URL, action URL, resource URL etc). In this article, we will use this class to create resource URL programmatically.

Add following code in ResourceURLByJavaAPIPortlet class.

Explanation

First I created a log variable. LogFactoryUtil in liferay is used to create log variable. This log variable is used to print various logs in server console.

Render method

  • I will create resource URL in render method and set it to request attribute. This will be used to make resource URL call with Ajax in JSP.
  • The code written in render method is used to generate resource URL programmatically.
  • First I took ThemeDisplay (provided by Liferay out of the box) object from request parameter.
  • create method of PortletURLFactoryUtil class is used to construct resource URL programmatically. It takes following parameters
    • PortletRequest :- RenderRequest is extending PortletRequest so you can pass it.
    • PortletName (String):- p_p_id of portlet whose serveResource method we want to be called by generated resource URL. To get p_p_id, you can either use
      1. 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.
      2. PortalUtil.getPortletId(renderRrequest)
    • Note:- In either of above two cases, it returns p_p_id of  current portlet. In case if you wish to call serveResource method of another portlet, you need to pass p_p_id of that portlet here.
    • plid (long):- It represent page layout id. (PK for Layout table).
      • plid represents layout id (PK) of liferay page on which the target portlet (of whose serveResource method is called by generated resource URL) is placed.
      • themeDisplay.getPlid() is used to get plid of liferay page for current portlet.
      • In target portlet is different that the current portlet, you need to pass plid of the layout on which target portlet is placed.
    • lifecycle(String):- Type of generated Portlet URL is decided based on value of this parameter.
      • If PortletRequest.RENDER_PHASE is passed,then this Portlet URL works as render URL.
      • If PortletRequest.ACTION_PHASE is passed, then this Portlet URL works as action URL
      • If PortletRequest.RESOURCE_PHASE is passed,then this Portlet URL works as resource URL
      • Since we need to generate resource URL, I gave PortletRequest.RESOURCE_PHASE.
  • PortletURLFactoryUtil.create() method returns the object of type PortletURL which represent resource URL in our case.
  • Note:- Since this is resource URL which would be called through Ajax from JSP, there is no need to set window state or portlet mode.
  • Additional parameters
    • Additional parameters can be added by calling setParameter method of PortletURL. I have added one parameter called ‘sampleParam‘ with sample text.
    • I will return it back as response of serverResource method.

serverResource method

Add serveResource method in ResourceURLByJavaAPIPortlet so that finally it looks like as below snippet.

  • serverResource method represent the serve resource phase / lifecycle of portlet.
  • This method will be called you make a call to resource URL
  • I am simply taking samplePara, display it into log, and putting on response which will be sent back to browser.
  • At the end, calling super.serveResource() method

In view.jsp file, we will use this resourceURL to call serveResource method. Add following code into view.jsp

Explanation:

  • added anchor tag and call java scrip function callResourceUrlByJavaAPI. which ultimately calling processAjax function with resourceUrlByJavaAPI resourc url as parameter. This is the same resource URL that we created in render method and added into request attribute.
  • Actual ajax call is made in processAjax function.
  • When you click this anchor tag, it will call resource URL and ultimately call serverResource method.
  • You should aware that resource URL should always be called from Ajax. You also can get some more information from this link.

 

Place 2: resource URL programmatically in JSP

You can create resource url in JSP. Add following code so that view.jsp finally looks as below.

Explanation:-

  • renderResponse is the implicit object available in jsp for Liferay portal.
  • this object is used to create various urls (render url, action url and resource url) in JSP.
  • I  have used renderResponse.createResourceURL() method to get resource url
  • Note:- return type is of ResourceURL
    • unlike PortletURLFactoryUtil.create() method which returns object of type PortletURLrenderResponse.createResourceURL() method returns object of type ResourceURL which you should take in consideration.
  • Once resourceURL is created, I just added one parameter.
  • You need to write this code in scriplet.
  • I have added one more javascript function callResourceUrlByFromJSP() from which I am calling processAjax function and passing the resourceURL which I have created in scriptlet above.

and you are done. When you deploy, it looks like below screenshot.

 

 

resource URL programmatically - final portlet output

  • The first link is of resource URL that we created in render method with Java API
  • second link is of resource URL that we created in JSP
  • When you click first link, you will get the parameter that you set while creating it in render method of portlet class as below.

resource URL programmatically - resource url with java api

  • When you click second link, you will get the parameter that you set while creating it in JSP

resource URL programmatically - resource url with JSP

And you are done. Please share your thoughts on creating resource URL programmatically in comment section.

Summing Up

  • Resource URL can be used to call serve resource phase of the portlet.
  • Programmatically you can create resource URL either from portlet class or from JSP
  • Programmatically created Resource URL can point to other portlet as well.

 

Download

In case if you don’t have Liferay developer studio 2.x, You can use Liferay IDE 3.1.2 GA3 which ships with Eclipse Neon 3. Download the maven project structure and source from Git Hub Project

 

Share This Post

Post Comment