Resource URL by liferay-portlet:resourceURL Tag

liferay-portlet:resourceURL is a custom tag provided by Liferay to create resource URL in Portlet JSP

liferay-portlet:resourceURL tag is used to create a resource URL. It works similar to portlet:resourceURL but provides some extra features. For example, if you wish to call the resource URL of another portlet that is placed on a different liferay page, then you can use this tag.

Resource URL by liferay-portlet:resourceURL Tag

In this article, I will be showing how to create a resource URL by liferay-portlet:resourceURL tag and how it’s different than portlet:resourceURL.

I would prefer to use Liferay MVC Portlet to demonstrate this because it’s simple to use. Give project name resourceURL-by-liferay-tag and -portlet is appended by Liferay IDE while creating a project. Give portlet class as com.opensource.techblog.portlet.ResourceURLByLiferayTagPortlet. The project structure should look like the below screenshot.

Project Structure to create resource URL by liferay-portlet:resourceURL

Open portlet class ResourceURLByLiferayTagPortlet.java and add the following code.

Explanation

  • I override serveResource method and added some text in the PrintWriter object, and flushing it. This text will be sent back to the client.
  • Finally, I put super.serveResource, which call the serveResource method of parent(MVCPortlet) class, and do low-level jobs. This is mandatory.

Now, add the following code in the view.jsp.

Explanation

  • resourceURL can be created by liferay tag liferay-portlet:resourceURL. To avail this tag, you need to add <%@ taglib uri=”http://liferay.com/tld/portlet” prefix=”liferay-portlet” %> taglib in your view.jsp.
  • Along with this, I also added portlet taglib (<%@ taglib uri=”http://java.sun.com/portlet_2_0″ prefix=”portlet” %>) to avail portlet:defineObjects tag. This tag is used to make portlet implicit objects available to this JSP.
  • Next, I created a resource URL by liferay-portlet:resourceURL tag. var attribute is used to create a page-scoped variable which holds the generated resource URL. You can refer to the resource URL by this variable in the other part of the page(Jsp) in EL (Expression Language).
  • In the end, I have defined one javascript function called callServeResource(). In this JS function, I am making an ajax call with generated resourceURL. You may like to know why you need to make a resource URL call in Ajax.
  • I have used the AUI module aui-io-request to make Ajax call. On the success of the ajax call, I am showing the result in JS alert.
  • resourceURL basically calls serveResource method defined in the portlet. On completion of the Ajax call, it will show the text that you passed in the serveResource method.

Save the view.jsp, portlet class, deploy the portlet and place it on some page. You will see one link. Click on it, and you will see the output (text that you passed in the serveResource method) in JS alert.

If you observe, resourceURL created by liferay-portlet:resourceURL and portlet:resourceURL are identical. However, liferay-portlet:resourceURL provides additional functionality.

Main differences between portlet:resourceURL and liferay-portlet:resourceURL

The main difference is the resource URL created by portlet:resourceURL tag can only point to the same portlet. In case if you wish to point to a different portlet that is placed on some different liferay page, you need to create a resource URL by liferay-portlet:resourceURL tag. To achieve this, you need to set plid and portletName attribute of liferay-portlet:resourceURL tag.

portletName:- Set portletName to the name of a portlet other than the current portlet. This allows you to point the resource URL to another portlet.

plid:- Set plid to the plid of another layout on which the target portlet is placed. This allows you to point the resource URL to the same/another portlet on another layout.

Apart from this, if you wish to encrypt all parameters you pass in resourceURL, you can set the encrypt attribute of liferay-portlet:resourceURL to true.

Summing Up

  • resource URL can be created by liferay tag liferay-portlet:resourceURL.
  • liferay-portlet:resourceURL works similar to portlet:resourceURL plus provides additional functionality.
  • If you wish to point resource URL on a different portlet (than current) placed on the same or different liferay page, you need to create a resource URL with liferay-portlet:resourceURL tag.

Download Portlet Source – (version Liferay 6.2)

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.

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.