Resource URL is used to retrieve images, XML, JSON or any other type of resource. Resource URL invokes the serveResource() method of portlet. This is helpful for operations like dynamically getting the content from server(using AJAX), downloading a file, etc.

After serve resource call is handled, it will simply returns response back to client side and no other methods are executed. Hence we have to use AJAX  method while making a serve resource call to fetch data from server side.

Here is a simple resource URL example which makes a 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="" prefix="liferay-portlet" %>
  • Add the following code to view.jsp file. Here we have a anchor tag, on clicking the link, java script method will be called. This method sends a AJAX request to action calss along with serve resource parameters. Response text will be printed on the screen.
<%@ taglib uri="" prefix="portlet" %>
<%@ taglib uri="" 
prefix="liferay-portlet" %>

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

<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){'', {
 dataType: 'json',
 on: {
   success: function() {
     var data = this.get('responseData');
   failure: function(){alert('Error');}
  • To handle the resource URL request, we have to override the serveResource method in action class.
  • Add the following code in action class. Here, we are getting request parameter and creating a JSON object and sending back the response to client side.
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 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);
  • We can also set different attributes while creating resource URL like: escapeXml, id, plid(while calling resource of another portlet), windowSate, etc.
  • Following is the output :



Also Check Liferay Action URL and Liferay Render URL.

Check out all Liferay related posts.

Note: Version used: Liferay 6.2 + Apache Tomcat 7.