create action URL By Liferay tag (liferay-portlet:actionURL) in JSP

liferay-portlet:actionURL is a custom tag provided by Liferay. This tag is used to create action URL.

liferay-portlet:actionURL is one of the very useful tag provided by Liferay. In this article, I will be showing how this tag is used to create actionURL in JSP. Action URL created by this tag works similarly as action URL created by PortletURLFactoryUtil class Let’s see this.

actionURL by liferay-portlet:actionURL Tag

First you need to create Liferay MVC Portlet. Give plugin project name as actionURL-by-LiferayTag and “-portlet” will be appended by Liferay IDE during project creation. Give portlet class as com.opensource.techblog.portlet.ActionURLByLiferayTagPortlet. The project structure should look like below screenshot.liferay-portlet:actionURL - Project-structure

Now, add following code in ActionURLByLiferayTagPortlet.java

 Explanation

  • First I have create log variable – _log. In liferay LogFactoryUtil class is used to get log for current class. This variable will be used to display logs in server console.
  • I have override the render() method. This method simply showing sample logs.
  • Next, I have defined action method actionMethodFromLiferayTag. In this action method, I am reading request parameter (ParamUtil is used to read request parameter) and displaying it in log. This action method will be called when user clicks on action URL (generated by liferay-portlet:actionURL tag in JSP)

Next, add following code to view.jsp

 Explanation

  • I have added Liferay-portlet:actionURL tag in JSP. This tag is used to create action URL.
  • To avail this tag, you need to add following taglib definition
    • <%@ taglib uri=”http://liferay.com/tld/portlet” prefix=”liferay-portlet” %>
  • Attributes of liferay-portlet:actionURL tag is similar as portlet:actionURL tag. The difference between these two tags (liferay-portlet:actionURL and portlet:actionURL ) are as below
    • Action URL created with <portlet:actionURL> can point to current portlet only.
    • Action  URL created with <(liferay-portlet:actionURL> can point to other portlet too. For this you need to give value of plid and portletName attribute for target portlet. 
    • While using <liferay-portlet:actionURL> tag,if you do not provide plid and portletName then (by default) it will take current portlet’s plid and portletName. Means such action URL (if plid and portletName are not provided then) points to current portlet only.
  • You can set window state and portlet mode similarly <portlet:actionURL>
  • We can also pass additional parameter with <liferay-portlet:param> tag similarly <portlet:param> tag.
  • var attribute is used to hold this action URL so that we can refer it anywhere in JSP.
  • At the end we created new link and set its href with this variable (var attribute defined in <liferay-portlet:param> tag) which holds action URL.

Save view.jsp and deploy the portlet. Refresh the page and you will see this link. When you click this link, our action method will be called and you will see the parameter’s (sampleParam) value (“This is sample param value”) is printed in server console.

Summing Up

  • <liferay-portlet:actionURL> liferay tag used to create action URL
  • We can set portlet mode and window state while creating actionURL
  • We can pass additional parameter by <liferay-portlet:param> child tag (just like <portlet:param> in <portlet:actionURL>)
  • actionURL created by <liferay-portlet:actionURL> tag can points to same as well as other portlets.
  • If you wish to point other portlet’s action method then you need to pass that portlet’s plid and portletName.
  • You may visit this link to understand render and action phase of portlet.

Following are other possible ways to create action URL in Liferay

Download Source

Share This Post

Post Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.