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.

 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

 Explanation

  • actionURL is used to call portlet action method. portlet:actionURL tag is used to create actionURL.
  • portlet:actionURL tag takes 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 page. 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 following logs in server console.

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

 

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

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

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

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

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 form (each having pair of text box and submit button). Give some value like [email protected] in second text box and click on second submit button. This time second action method is called followed by render() method.

So far we have seen that action URL (created by portlet:actionURL tag) points to action method in portlet. It identify correct action method by its name. In other words, value of name attribute in portlet:actionURL tag must be equal to 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 meta data to action method.

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

Quick observation

  • I have added ProcessAction annotation to third action method – sampleActionMethod.
  • 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 action URL to this action method, then you need to set value of name attribute of portlet:actionURL tag same as the name attribute of ProcessAction annotation.
    • In short, you can access this action method by following code in JSP

  •  Thumb rule
    • if you are not defining ProcessAction annotation then action method’s name and the name attribute of portlet:actionURL tag must be same.
    • If you are using annotation then name attribute of ProcessAction annotation and name attribute of portlet:actionURL tag must be same.
    • This is how portlet action method invocation performed for 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 annotation is defined)
  • Variable name defined by var attribute of portlet:actionURL must be unique in given JSP.
  • If corresponding action method is not defined in portlet then it throws an error.
  • You may refer this link to know more about render and action phase

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.

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.