Liferay Resource URL example

We can use the Liferay resource URL to retrieve images, XML, JSON, or any other type of resource. Resource URL invokes the serveResource() method of the portlet.

Resource URL is helpful for operations like dynamically getting the content from the server(using AJAX), downloading a file, etc.

The serve resource handler directly returns the response to the client-side without calling the render phase.

We use the AJAX method while making a serve resource call to fetch data from the server side.

Version details: Liferay 6.2 + Apache Tomcat 7.

Implementing the Resouce URL

Here is a simple resource URL example that makes an AJAX request to portlet action class and gets a response in JSON form.

Import the liferay-portlet tag library to view.jsp file as shown below.

<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>

Add the following code to view.jsp file.

Here we have an anchor tag that calls the javascript method on clicking the link. This method sends an AJAX request to the action class along with the serve-resource parameters.

Finally, UI displays the received response.

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/portlet" 
prefix="liferay-portlet" %>

<liferay-portlet:resourceURL var="myResourceURL" >
 <portlet:param name="myParam" value="myValue"/>
</liferay-portlet:resourceURL>

<a href="#" onClick="callServeResource()">Click here</a>
 
<h4>Serve resource response will be printed here: 
<span id="resHolder"></span></h4>


function callServeResource()
{
AUI().use('aui-io-request', function(A){
 A.io.request('', {
 dataType: 'json',
 on: {
   success: function() {
     var data = this.get('responseData');
     document.getElementById('resHolder').innerHTML=data.myValue;
   },
   failure: function(){alert('Error');}
 }
 })
}); 
}

To handle the resource URL request, we have to override the serveResource method in the action class.

Add the following code in the action class.

In this class, we are extracting the request parameters from the response object and then creating a JSON object.

Finally, the created response object returns the response to the client.

package com.test;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import java.io.IOException;
import java.io.Writer;
import javax.portlet.PortletException;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

public class ResourceURLPortlet extends MVCPortlet {
 public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws PortletException, IOException {
 String param = ParamUtil.getString(resourceRequest, "myParam");
 Writer wtr = resourceResponse.getWriter();
 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
 jsonObj.put("myValue", param);
 wtr.write(jsonObj.toString());
 wtr.flush();
 }
}

We can also set different attributes while creating resource URLs like escapeXml, id, plid(while calling resource of another portlet), windowSate, etc.

Testing the output

Run the application, and we should be able to get the below screen with a resource URL link.

resource1

On clicking the link, the value is rendered through the AJAX call, as shown below.

resource2

Also, Check Liferay Action URL and Liferay Render URL.

Conclusion

In this article, we learned about Liferay resource URL and how to implement it.