create action URL programatically in Portlet class and JSP

Liferay provides PortletURLFactoryUtil class to create action URL programatically (Java API). Such action URL can point to different portelt which is placed on either same or different page.

Liferay provides facility to create action URL programatically through Java API. This is useful when you want to make a call to action method of portlet B from portlet A.

action url can be created in JSP by portlet tag. Such action url can point to action method of same portlet only. So in case if you want to call action method of different portlet, you need to create action url programatically by Java api.

In this article I will be showing you how to create action URL programatically through Java API. You need to create LiferayMVC portlet with plugin project name as actionURL-by-java-api and ‘-portlet’ will be appended by eclipse. I gave portlet class as com.opensource.techblog.portlet.ActionURLByJavaAPIPortlet. The project structure looks like below screenshot.

Project Structure - create action URL programatically

create action URL programatically

There are two places where you can create action URL programatically with Java API

Creating action URL by PortletURLFactoryUtil in Portlet class

PortletURLFactoryUtil class (provided by Liferay) is used to create various portlet URLs (like render URL, action URL, resource URL etc). In this article, we will use this class to create action URL programatically.

Add following code in ActionURLByJavaAPIPortlet class.

//define log for this class
	private static final Log _log = LogFactoryUtil.getLog(ActionURLByJavaAPIPortlet.class.getName());

	public void render(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {
		  ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
		  PortletURL actionUrl =  PortletURLFactoryUtil.create(request, themeDisplay.getPortletDisplay().getId(), themeDisplay.getPlid(), PortletRequest.ACTION_PHASE);
		  actionUrl.setParameter("javax.portlet.action", "actionMethodByJavaApi");
		  actionUrl.setParameter("sampleParam", "This parameter comes from Action URL generated with Java API");
		  request.setAttribute("actionUrlByJavaAPI", actionUrl.toString());
		super.render(request, response);

	public void actionMethodByJavaApi(ActionRequest request, ActionResponse response)
			throws IOException, PortletException, PortalException, SystemException{
		String sampleParam = ParamUtil.get(request, "sampleParam", "defaultValue");"Sample Param is ::"+sampleParam);


  • First I created a log variable. LogFactoryUtil in liferay is used to create log variable. This log variable is used to print various logs in server console.

         Render method

  • We will create action URL in render method and set it to request attribute. In JSP, we will take this and set it in anchor tag.
  • The code written in render method is used to generate action URL programatically.
  • First I took ThemeDisplay object from request parameter which is made available for us by Liferay.
    • ThemeDisplay is a util class provided by Liferay. It provides current theme and some other generic information.
    • Configure Liferay source to get more information about ThemeDisplay object.
  • PortletURLFactoryUtil class is used to construct action URL programatically.
  • I am calling create method on PortletURLFactoryUtil class. This method takes following parameters
    • PortletRequest :– RenderRequest is extending PortletRequest so I have pass it.
    • PortletName (String):- p_p_id of portlet whose action method is called by generated action URL. I have used themeDisplay.getPortletDisplay().getId().
      • themeDisplay.getPortletDisplay() returns object of type PortletDisplay which represents Portlet content.
      • getId() method of PortletDisplay returns portlet id which is nothing but p_p_id.
      • By default themeDisplay.getPortletDisplay().getId() returns p_p_id of current portlet. If you wish to call other portlet’s action method then you need to pass that portlet’s p_p_Id in this parameter.
    • plid (long):- It represent page layout id. (PK for Layout table). 
      • Layout in liferay represent liferay page and its information is stored in Layout table. 
      • plid represents layout id of liferay page on which the target portlet (of whose action method is called by generated action URL) is placed.
      • I have used themeDisplay.getPlid() which returns plid of liferay page for same portlet.
      • In case if you wish to call other portlet’s action method then you need to pass plid (layour Id) of page on which that portlet is placed.
    • lifecycle(String):-  
      • Type of generated Portlet URL is decided based on value of this parameter.
        • If PortletRequest.RENDER_PHASE is passed,then this Portlet URL works as render URL.
        • If PortletRequest.ACTION_PHASE is passed, then this Portlet URL works as action URL
        • If PortletRequest.RESOURCE_PHASE is passed,then this Portlet URL works as resource URL
      • Since we need to generate action URL, I gave PortletRequest.ACTION_PHASE.
        • Note:- PortletRequest is an interface provided by portlet specification. It’s implementation is provided in portlet.jar (resides in tomcat/lib/ext path).
  • If you observed, PortletURLFactoryUtil.create method returns the object of type PortletURL which represent action URL.
  • You can set window state and portlet mode by calling setWindowState and setPortletMode methods respectively on PortletURL object.
  • You need to pay special attention to the most important thing as below.
  • Additional parameters can be added by calling setParameter method of PortletURL. Eventually these parameters are added to action URL represented by PortletURL. I have added one parameter called ‘sampleParam‘ with sample text. I will show this parameter in portlet action method when this action URL is called from JSP.
  • At the end, I am storing the portlet URL in request scope.

     Action Method

  • I have defined one action method called ‘actionMethodByJavaApi‘.This action method will be called when user clicks the action URL from view.jsp (We will set action URL in view.jsp which we created in above render method)
  • This action method simply reading request parameter (by ParamUtil class) and printing in logs.

Add following code in view.jsp

<%@ taglib uri="" prefix="portlet" %>
<portlet:defineObjects />
<a href="${actionUrlByJavaAPI}">Action Url created by Java API in Portlet Class</a>


  • You can observe that I am reading the action URL through EL (Expression Language) and passing it to href of anchor tag.
  • When a user clicks on this, it will call the portlet action method and the user will see the parameter that was added in action URL (created in render method) in server console.

Creating action URL programatically by renderResponse (implicit object )in view.jsp

This is the second place where you can create action URL programmatically. I have added few more lines in view.jsp so finally it looks like below snippet.

<%@page import="javax.portlet.PortletURL"%>
<%@page import="com.liferay.portal.kernel.portlet.LiferayPortletMode"%>
<%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%>
<%@ taglib uri="" prefix="portlet" %>

<portlet:defineObjects />

<a href="${actionUrlByJavaAPI}">Action Url created by Java API in Portlet Class</a>
    PortletURL actionUrlFromJSP = renderResponse.createActionURL();
	actionUrlFromJSP.setParameter("javax.portlet.action", "actionMethodByJavaApi");
	actionUrlFromJSP.setParameter("sampleParam", "This portletULR is created with API in JSP");
<a href="<%=actionUrlFromJSP%>">Action Url created by JAVA API in JSP</a>


  • In scriptlet, I am using renderResponse.createActionURL() method.
    • renderResponse is implicit object which can be available by <portlet:defineObjects /> tag. This object is used to create action URL programatically in JSP.
    • createActionURL() method of renderResponse returns objects of type PortletURL (Which represent action URL in our case).
  • Window state (normal, maximize, minimize, pop up etc) and portlet mode (view, edit, help etc) can be set by calling setWindowState and setParameter methods respectively on PortletURL object.
  • After that, I am adding parameters.
    • Just like creating action URL in portlet class, I am setting one parameter called ‘javax.portlet.action‘ to ‘actionMethodByJavaApi‘. This is because I would like to call actionMethodByJavaApi action method when the user clicks on this action URL.
    • In other words,value of  ‘javax.portlet.action‘ represent the action method name (or name attribute or @ProcessAction annotation defined for that action method).
  • Next, I am simply adding parameter which I am reading in action method (actionMethodByJavaApi).
  • At the last, I am pointing this action URL to href for anchor tag. When user clicks on it, corresponding action method is called.
  • Save view.jsp and deploy the portlet. You will see two link. First link is action URL that we have created in portlet class. Second link is action URL which we created in view.jsp.

Note:- Action URL created in view.jsp will point to current portlet only. If you wish the action URL points to action method of different portlet, you need to create it in portlet class as shown in the first option.

Summing UP

  • Liferay provides a way to create action URL programatically in either portlet class or in JSP.
  • PortletURLFactoryUtil class provided by Liferay is used to create action URL programatically in portlet class.
  • action URL programatically created by PortletURLFactoryUtil can point to other portlet’s action method as well.
  • renderResponse (JSP implicit object) is used to create action URL in JSP.
  • action URL created by renderResponse implicit object can point to current portlet’s action method only.
  • You may wish to visit this link to know action and render phase of portlet.

Following are other possible ways to create action URL in Liferay

Download Source

Download Source

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.

6 Comments to “create action URL programatically in Portlet class and JSP”

    1. Hi Saurabh,

      Thanks for Appreciation and welcome to Tech blog. Feel free to ask question / give feedback


    1. Hi Devang,

      Thanks for Appreciation and welcome to Techblog. Feel free to ask questions / give suggestions.


    1. Hi Sanket,

      Welcome to Techblog and thanks for appreciation. Feel free to give suggestions / ask questions.


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.