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 resource URL. It works similar to portlet:resourceURL but provides some extra feature. For example, if you wish to call resource URL of other portlet which is placed on 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 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 project. Give portlet class as com.opensource.techblog.portlet.ResourceURLByLiferayTagPortlet. Project structure should look like below screenshot.

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

open portlet class ResourceURLByLiferayTagPortlet.java and add following code.

Explanation

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

Now, add following code in 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 resource URL by liferay-portlet:resourceURL tag. var attribute is used to create page scoped variable which holds generated resource URL. You can refer resource URL by this variable in other part of page(Jsp) in EL (Expression Language).
  • At the end I have defined one javascript function called callServeResource(). In this JS function, I am making ajax call with generated resourceURL. You may like to know why you need to make resource URL call in Ajax.
  • I have used AUI module aui-io-request to make Ajax call. On success of ajax call, I am showing the result in JS alert.
  • resourceURL basically calls serveResource method defined in portlet. On completion of Ajax call, it will show the text that you passed in serveResource method.

Save 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 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, resource URL created by portlet:resourceURL tag can point to same portlet only. In case if you wish to point different portlet which is placed on some different liferay page, you need to create 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 same/another portlet on another layout.

Apart from this, if you wish to encrypt all parameter that you are passing in resourceURL, you can set 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 different portlet (than current) which is placed on same or different liferay page, you need to create resource URL with liferay-portlet:resourceURL tag.

Download Portlet Source – (version Liferay 6.2)

Share This Post

Post Comment