Resource URL by portlet tag portlet:resourceURL

portlet:resourceURL is one of the custom tag provided by default portlet implementation (resides in portlet.jar).This tag is used to create resource URL in portlet JSP.

In this article, I will be showing you how to create resource URL by portlet tag. Sun provides implementation of portlet API which resides in portlet.jar file. This jar file contains classes and interfaces under javax.portlet pacakage.

This implementation (given in portlet.jar file under tomcat/lib/ext path) provides certain custom tags to create Resource URL. Resource URL is used to execute serve resource phase (serveResource lifecycle method) of portlet.

portlet-resourceURL_tag - portlet:resourceURL

Create resource URL by portlet:resourceURL tag

I am using Liferay MVC portlet to demonstrate creation of resource URL by portlet tag. You can refer my blog on Creating MVC Portlet in Liferay to create project structure.

I gave plugin project name as resourceURL-by-portlet-tag and -portlet will be appended by Liferay IDE while creating Liferay plugin project. I gave portlet class as com.opensource.techblog.portlet.ResourceURLByPortletTagPortlet.

At this point, project structure should look like below screenshot.

project_structure - portlet:resourceURL

 Add following code in portlet class –ResourceURLByPortletTagPortlet.java

Explanation

  • In first line, I am creating log object which will is used to print logs for this portlet. LogFactoryUtil class provided by liferay is used to get log object.
  • Next, I overridden render method. It represent render phase of portlet. Inside this render method, there is on log statement.
  • When serve resource phase is executed by portal, it won’t call render (or any other portlet ) phase. So you won’t see log statement defined in render method when serverResource method finish its execution.
  • Next, I override serveResource method. Default implementation of serveResource method is provided in GenericPortlet.
  • The code written inside serveResource method is more or less looks like servlet code. What we are doing is, getting PrintWriter object, adding some text and flush it. This is what you are doing in case if you send some output from servlet.
  • At the last super.serveResource will call super class’s (MVCPortlet) serveResource method which will do all low level things. It’s important that you make a call to super.serveResource at the last in serveResource method of your custom portlet. Otherwise it won’t work properly.

serveResource method represent serve resource phase of portlet. It is called when user clicks on resourceURL from JSP. To see how to create resource URL in JSP, update your view.jsp with below code snippet.

Explanation

  • portlet:resourceURL tag is used to create resourceURL in JSP. To avail this tag, you have to define <%@ taglib uri=”http://java.sun.com/portlet_2_0″ prefix=”portlet” %> taglib in your Jsp.
  • <portlet:defineObjects /> tag is used to make portlet implicit objects available in JSP.
  • portlet:param inside portlet:resourceURL is used to pass additional parameter which can be access in serveResource method by resourceRequest.getParameter api.
  • var attribute for portlet:resourceURL is used to define variable which holds the generated resource URL. You can refer the generated resource URL by this variable in other part of page(Jsp).
  • At the last, I created one anchor link and set it’s href to generated resource URL (By referring variable in EL which holds resource URL).

Save portlet class and view.jsp, deploy the portlet and place it on some page. You will see one link. Click on it and you will see output like below screen shot.

resource_URL_Output - portlet:resourceURL

Why you need to call resourceURL in Ajax ?

You will see that whole portal output vanished (header, footer etc). You will see the text (or html) you set in serveResource method on page. Why this happened ? What is wrong here ?

Let’s recall that portal will not call any other phase (lifecycle method) after execution of serve resource phase. Due to this behavior, portal will send the response ( generated in serveResource method ) directly to client.

If you trigger resourceURL in ajax call, you will get its response in dynamically manner which is more managable. (e.g. updating DOM element with output or calling JS to update page html with output).

This way whole page is not vanished. You will get the output of serveResource() method without refreshing whole page. To see how you can achieve this, change view.jsp code as below snippet.


Explanation

  • I have created java script function called callServeResource() in which I am making ajax call.
  • AUI Javascript library is used to make Ajax call. You can refer my blog on making Ajax call with AUI in Liferay Portlet
  • I am calling callServeResource javascript function with onclick event of link. Since we are calling resourceURL in ajax, I am setting # to href.
Save the view.jsp, deploy the portlet and refresh the page. Click on the link and you will see below output.
resourceURL_In_Ajax - portlet:resourceURL

 Summing Up

  • resource URL can be created by portlet tag portlet:resourceURL in Jsp.
  • serveResource method will be called when user clicks on resource URL
  • Since portal is not calling any other portlet phase after completion of serve resource phase, you need call resource URL in Ajax.

Download Portlet Source – (version Liferay 6.2)

Download Source

Share This Post

3 Comments - Write a Comment

Post Comment