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

  • action: This phase is invoked from one portlet at a time. This is used for activities, which includes database entity creation/modification and should not be repeated.
  • render: This phase is always invoked for all portlets after action phase.

Liferay portlet provides three different types of URLs to support render and action phases. those are : Action URL, Render URL and resource URL.

In this example, we will learn how to use action URL using portlet tag library and Java code.

  • Initially, we have to import portlet tag library using following import statement in our jsp file:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
  • Add the following code to jsp page. Here, we are using portlet tag library to create action URL. This URL can be used to call action 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 which is invoked by the action URL.
    • var: This attribute is a name of the URL, which can be used to call action URL.
    • escapeXml: This attribute can be set to true, which replaces XML characters(ie: , etc.) to its  equivalent character entity codes. Default value is true.
    • portletMode: This attribute is used 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 action.
    • copyCurrentRenderParameters: Setting this attribute to true will include all current render parameters to action URL while invoking action.
    • windowState : This is to set liferay window states like normal, maximized, minimized or pop_up.
  • We can also add parameters to action URL using  tag inside action URL tag as shown above.
  • Created action URL can be now used by mentioning the name attribute in any
    , 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 action request and printing it in console.
  • Following code shows the same example with using Java code to create 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);
%>
  • Following is the output. After successful action call, default success message will be displayed on the screen.

actionbutton

actionsuccess

  • And the Console log:

actionconsole

Check out all Liferay related posts.

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