create action URL by Portlet Tag (portlet:actionURL) in JSP

actionURL is used to call portlet’s action method. portlet:actionURL tag is a custom tag provided by default portlet implementation (resides in portlet.jar) to construct actionURL

portlet:actionURL tag is used to call portlet action method which represents portlet action phase. In this article, we will see how to write portlet action method, how to call it and and how it works. First you need to create liferay MVC Portlet. I gave portlet name as ActionURLByPortletTagPortlet under com.opensource.techblog.portlet package. Project structure looks like below screenshot.

Create actionURL by portlet:actionURL tag in JSP - project structure

 

action URL by portlet:actionURL tag

Add following code in ActionURLByPortletTagPortlet class.

package com.opensource.techblog.portlet;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.ProcessAction;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

public class ActionURLByPortletTagPortlet extends MVCPortlet {

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

        @Override
        public void render(RenderRequest request, RenderResponse response)
                throws PortletException, IOException {
            _log.info(" This is render method of ActionURLByPortletTagPortlet");
            super.render(request, response);
        }
        
        public void actionMethod1(ActionRequest request, ActionResponse response)
                throws IOException, PortletException, PortalException, SystemException{
            String userName=ParamUtil.get(request, "userName", "");
            _log.info("userName is ==>"+userName);
        }
}

 Explanation

  • In first line, I have defined  log for this class. LogFactoryUtil is used to define logs.
  • Next, I have overridden render () method and placed logs to know when render() method is called.
  • After that I have defined actionMethod1 method. This method takes ActionRequest and ActionResponse objects as parameter. This is our action method.
  • unlike render() method, you no need to override any method to define action method.
  • You can give any valid name for action method.
  • In this action method, I am reading request parameter and showing in logs.

Our action method is ready. You can call this action method by actionURL from JSP. Add following code in view.jsp

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

<portlet:actionURL name="actionMethod1" var="actionURLByPortletTagURL">
</portlet:actionURL>

<form action="${actionURLByPortletTagURL}" method="post">
    UserName :-<input type="text" name="userName"><br>
    <input type="submit" value="Submit"> 
</form>

 Explanation

  • actionURL is used to call the portlet action method. portlet:actionURL tag is used to create actionURL.
  • portlet:actionURL tag takes the following attributes
    • name
      • It represents the name of the action method we defined in our portlet.
      • I gave actionMethod1 to call action method(actionMethod1) defined in our portlet.
    • var
      • It defines page-scoped JSP variable to hold actionURL generated by  portlet:actionURL tag. It can be access by expression language in other part of JSP.
      • I have used actionURLByPortletTagURL as var. This means, you can refer this actionURL by ${actionURLByPortletTagURL} in any part of JSP.
  • Next, I have defined HTML form and point its action to actionURL created in portlet:actionURL tag by ${actionURLByPortletTagURL}. Notice that value of var attribute is used in EL(Expression Language) to get generated actionURL.
  • Make sure the HTML form’s method is post always.

Save portlet class and view.jsp. Deploy the portlet and place it on some pages. You will see one text box and one submit button. Give some value ex. ‘test’ in text box and click on submit button. You will see the following logs in the server console.

Create actionURL by portlet:actionURL tag in JSP - render method is called after action method

 

The first log is coming from the action method. It simply prints the request parameter. The second log is coming from the render method. You can see that the portlet container calls the render() method soon after the action method is completed.

Portlet specification allows adding multiple action methods. This means you can define multiple action methods in the same portlet. Let’s add one more action method.

I have added an additional action method in the portlet class as per the below code snippet.

public void actionMethod2(ActionRequest request, ActionResponse response)
                throws IOException, PortletException, PortalException, SystemException{
            String userName=ParamUtil.get(request, "email", "");
            _log.info("email is ==>"+userName);
        }

The second action method name is actionMethod2. This method is also reading request parameters and printing in logs. You need to define separate actionURL in view.jsp to call this action method as per the below snippet.

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

<portlet:actionURL name="actionMethod1" var="actionURLByPortletTagURL">
</portlet:actionURL>

<portlet:actionURL name="actionMethod2" var="updateEmailAddressURL">
</portlet:actionURL>

<form action="${actionURLByPortletTagURL}" method="post">
    UserName :-<input type="text" name="userName"><br>
    <input type="submit" value="Submit"> 
</form>
<br>
<br>
<form action="${updateEmailAddressURL}" method="post">
    Update Email :-<input type="text" name="email"><br>
    <input type="submit" value="Submit"> 
</form>

Explanation

  • I have defined second action URL (by additional portlet:actionURL tag) and set its name as actionMethod2(Same as portlet action method name) and var as updateEmailAddressURL.
  • Additional HTML form is added. This form’s action is pointing to second action URL created in portlet:actionURL tag by ${updateEmailAddressURL}. Notice that value of var attribute is used in EL(Expression Language) to get generated action URL.

save portlet class and view.jsp. Deploy the portlet and refresh the page. This time you will see two HTML forms (each having pair of the text boxes and submit button). Give some value like [email protected] in the second text box and click on the second submit button. This time second action method is called followed by the render() method.

So far we have seen that the action URL (created by portlet:actionURL tag) points to the action method in the portlet. It identifies the correct action method by its name. In other words, the value of the name attribute in portlet:actionURL tag must be equal to the portlet action method name.

In some cases, it’s required to have different values for name attribute in portlet:actionURL tag and portlet action method name. We can achieve this by defining extra metadata to the action method.

Add third action method in portlet class which looks like the below snippet.

@ProcessAction(name="actionMethod3")
public void sampleActionMethod(ActionRequest request, ActionResponse response)
                throws IOException, PortletException, PortalException, SystemException{
            _log.info("This is sampleActionMethod ...");
        }

Quick observation

  • I have added ProcessAction annotation to the third action method – sampleActionMethod.
  • The name attribute of ProcessAction annotation is actionMethod3.
  • What does this mean?
    • This time the mapping of action URL to portlet action method is created between name attribute of ProcessAction annotation and name attribute of portlet:actionURL tag.
    • In other words, If you want to point the action URL to this action method, then you need to set the value of the name attribute of portlet:actionURL tag same as the name attribute of ProcessAction annotation.
    • In short, you can access this action method by the following the code in JSP
<portlet:actionURL name="actionMethod3" var="sampleActionMethodURL">
</portlet:actionURL>
  •  Thumb rule
    • if you are not defining ProcessAction annotation then the action method’s name and the name attribute of portlet:actionURL tag must be the same.
    • If you are using annotation then the name attribute of ProcessAction annotation and name attribute of portlet:actionURL tag must be the same.
    • This is how the portlet action method invocation performed for the Liferay portlet.

Summing UP

  • portlet:actionURL is used to create action URL
  • action URL can points to portlet action method either by
    • name of action method or
    • name of ProcessAction annotation (If the annotation is defined)
  • Variable name defined by var attribute of portlet:actionURL must be unique in given JSP.
  • If the corresponding action method is not defined in the portlet then it throws an error.
  • You may refer this link to know more about the render and action phase

Following are other possible ways to create an 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.

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.