Search code examples
tabsliferayliferay-6

How to call controller method on click of tab in liferay ui tabs?


I am using liferay ui tabs in my project. I would like to call the controller on click of the tabs. I used portlerURL attribute for try but its not working.

Here is my code snippet :-

view.jsp

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet"%>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>


<liferay-theme:defineObjects />
<portlet:defineObjects />


<liferay-portlet:renderURL var="portletURL" />

<%
    //We must Specify a default value for tabs. In this example it is sunday. Else it //will throw an error.
    String tabValue = ParamUtil.getString(request, "tab", "sunday");
    String tabsURL = "/" + tabValue.trim() + ".jsp";
    String tabNames = "Sunday,Monday,Tuesday";
    String tabVal = "sunday,monday,tuesday";
    if (permissionChecker.isCompanyAdmin(themeDisplay.getCompanyId())) {
        tabNames += ",Admin";
        tabVal += ",admin";
    }

    PortletURL URL = renderResponse.createActionURL();
    URL.setParameter("click","tabClick");
%>

<liferay-ui:tabs names="<%=tabNames%>" tabsValues="<%=tabVal%>" param="tab" url="<%= portletURL %>" />

<c:import url="<%= tabsURL %>"></c:import>

Controller :-

import com.liferay.util.bridges.mvc.MVCPortlet;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

/**
 * Portlet implementation class TabDemoPortlet
 */
public class TabDemoPortlet extends MVCPortlet {

    public void sunday(ActionRequest req,ActionResponse res) {
        System.out.println("Hello Sunday!!!");
    }

    public void monday(ActionRequest req,ActionResponse res) {
        System.out.println("Hello Monday!!!");
    }

    public void tuesday(ActionRequest req,ActionResponse res) {
        System.out.println("Hello Tuesday!!!");
    }
}

All suggestions are welcome. Thanks in advance!!!


Solution

  • You have to create multiple actionURLs and use them in you liferay-ui:tabs. Some thing like this

    <portlet:actionURL name="sunday" var ="sundayURL"/>
    <portlet:actionURL name="monday" var ="mondayURL"/>
    <portlet:actionURL name="tuesday" var ="tuesdayURL"/>
    
    <liferay-ui:tabs names="<%=tabNames%>" 
       url0="<%=sundayURL/>"
       url1="<%=mondayURL/>"
       url2="<%=tuesdayURL/>"/>