Liferay Action URLs example

In this article, we will learn about Liferay action URLs and how to create them.

Portlets contain two types of execution phases, which are the action phase and the render phase.

  • action: Only one portlet can access this phase at a time. Activities like database entity creation or modification, activities that should happen only once, etc utilize Liferay’s action phase.
  • render: After the action phase execution the render phase renders the view.

Liferay portlet provides three types of URLs to support the render phase and action phase. These URL types are Action URL, Render URL, and resource URL.

In this example, we will learn to implement the Liferay action URLs using the portlet tag library.

Version used: Liferay 6.2 + Apache Tomcat 7.

To learn about how to create a Liferay portlet, please visit this article.

Creating liferay action URLs

Initially, we have to import the portlet tag library using the below import statement in our jsp file.

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

Add the following code to the jsp page. Here, we are using the portlet tag library to create an action URL. We can use this URL to call the action class inside the scope of this jsp page.

<portlet:actionURL var="myAction" name="myAction">
 <portlet:param name="actionParam" value="paramVal"/>
</portlet:actionURL>

Action URL tag also provides few attributes which are:

  • name: This attribute indicates the name of the action method.
  • var: This attribute is the name of the URL, which calls action URL.
  • escapeXml: This attribute can be set to true to replace the XML characters with their equivalent character entity codes. The default value is “true”
  • portletMode: We can use this attribute to set the portlet mode like view, edit, etc.
  • secure: Setting this attribute to “true” tells that the URL must use a secure connection while invoking the action.
  • copyCurrentRenderParameters: Setting this attribute to true will include all current render parameters to the action URL while invoking the action.
  • windowState: This is to set Liferay window states like normal, maximized, minimized, or pop_up.

We can also add parameters to the action URL using the tag inside the action URL tag as shown above.

Once the action URL is ready, we can use it on any form submits.

Now add the following code inside the action class, which handles the action URL request.

package com.test.action;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
public class LiferayActionURLPortlet extends MVCPortlet {
  public void myAction(ActionRequest actionRequest, ActionResponse actionResponse) 
    throws SystemException, PortletException {
    String actionParam = ParamUtil.getString(actionRequest, "actionParam","No value");
    System.out.println("Action Parameter::"+actionParam);
  }
}

Here we are retrieving the action parameter from the action request and printing it in the console.

The following code shows the same example using Java code to create an action URL.

<%
 ThemeDisplay td = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
 PortletURL actionURL = PortletURLFactoryUtil.create(request, td.getPortletDisplay().getId(), td.getPlid(), PortletRequest.ACTION_PHASE);
 actionURL.setParameter("actionParam", "paramVal");
 actionURL.setParameter("javax.portlet.action", "myAction");
 actionURL.setWindowState(LiferayWindowState.NORMAL);
%>

Testing the output

Following is the output of the Liferay action URL example application.

actionbutton

After a successful action call, we get the default success message on the screen, as shown below.

actionsuccess

Also, we can observe the console log below.

actionconsole

Conclusion

In this article, we learned about Liferay action URLs and how to create Liferay action URLs.

Liferay Action URLs example
Scroll to top

Discover more from ASB Notebook

Subscribe now to keep reading and get access to the full archive.

Continue reading