Sample code for geting different url in liferay.

Liferay developers often need to find different from render/servlet request.

 

Here below i share some code to get different url in liferay

 

Sample code:

 

        HttpServletRequest servletRequest = PortalUtil.getHttpServletRequest(request);

        String currentCompleteUrl = PortalUtil.getCurrentCompleteURL(servletRequest);

 

        try {

           

            String renderCurrentURL =    PortalUtil.getCurrentURL(request);

            String servletRequestCurrentURL = PortalUtil.getCurrentURL(servletRequest);

            String canonicalURL    = PortalUtil.getCanonicalURL(currentCompleteUrl, themeDisplay, themeDisplay.getLayout());

            String homeURL = PortalUtil.getHomeURL(servletRequest);

            String currentPageURL = PortalUtil.getPortalURL(themeDisplay)+themeDisplay.getLayout().getFriendlyURL();

            String portalUrl =    PortalUtil.getPortalURL(request);

            String servletRequestPortalUrl = PortalUtil.getPortalURL(servletRequest);

            String themedisplayPortalUrl  = PortalUtil.getPortalURL(themeDisplay);

            String themedisplaylayoutPortalUrl = PortalUtil.getPortalURL(themeDisplay.getLayout(), themeDisplay);

            String portalWebDir =    PortalUtil.getPortalWebDir();

            String portalLibDir = PortalUtil.getPortalLibDir();

           

            log.info("currentCompleteUrl: "+currentCompleteUrl);

            log.info("renderCurrentURL: "+renderCurrentURL);

            log.info("servletRequestCurrentURL: "+servletRequestCurrentURL);

            log.info("canonicalURL: "+canonicalURL);

            log.info("homeURL: "+homeURL);

            log.info("currentPageURL: "+currentPageURL);

            log.info("portalUrl: "+portalUrl);

            log.info("servletRequestPortalUrl: "+servletRequestPortalUrl);

            log.info("themedisplayPortalUrl: "+themedisplayPortalUrl);

            log.info("themedisplaylayoutPortalUrl: "+themedisplaylayoutPortalUrl);

            log.info("portalWebDir: "+portalWebDir);

            log.info("portalLibDir: "+portalLibDir);

           

        } catch (Exception e) {

            e.printStackTrace();

        }

Sample output:

 currentCompleteUrl: http://localhost:7070/organization?selectedCard=Organization&cardViewState=Listview

 renderCurrentURL: /organization?selectedCard=Organization&cardViewState=Listview

 servletRequestCurrentURL: /organization?selectedCard=Organization&cardViewState=Listview

 canonicalURL: http://localhost:7070/organization?selectedCard=Organization&cardViewState=Listview

 homeURL: http://localhost:7070/web/guest

 currentPageURL: http://localhost:7070/organization

 portalUrl: http://localhost:7070

 servletRequestPortalUrl: http://localhost:7070

 themedisplayPortalUrl: http://localhost:7070

 themedisplaylayoutPortalUrl: http://localhost:7070

 portalWebDir: /home/tariq/projects/projects/coorg/bundles/tomcat-7.0.42/webapps/ROOT/

 portalLibDir: /home/tariq/projects/projects/coorg/bundles/tomcat-7.0.42/webapps/ROOT/WEB-INF/lib/

Reference Blog:

http://tariqliferay.blogspot.com/2015/11/sample-code-for-geting-different-url-in.html

Enjoy.

Ajax form submit in liferay.

Sometimes liferay need to submit in ajax. Here below i share my experience for on ajax form submit in liferay.

You can download source code from here..

Below the view.jsp:

<%--

 

--%>

 

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>

 

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

 

<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>

<%@ taglib uri="http://liferay.com/tld/ddm" prefix="liferay-ddm" %>

<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>

<%@ taglib uri="http://liferay.com/tld/security" prefix="liferay-security" %>

<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>

<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>

<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>

 

<%@ page import="com.liferay.portlet.journal.service.*" %>

<%@ page import="com.liferay.portlet.journal.model.*" %>

<%@ page import="com.liferay.portal.kernel.workflow.WorkflowConstants" %>

<%@ page import="com.liferay.portal.kernel.language.LanguageUtil"%>

<%@ page import="com.liferay.portal.kernel.util.LocalizationUtil"%>

<%@ page import="com.liferay.portlet.journalcontent.util.JournalContentUtil"%>

<%@ page import="com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil"%>

<%@ page import="com.liferay.portal.util.PortalUtil"%>

<%@ page import="com.liferay.portal.kernel.util.PropsUtil"%>

<%@ page import="com.liferay.portal.util.PortletKeys"%>

<%@ page import="com.liferay.portal.kernel.util.CalendarUtil"%>

<%@ page import="com.liferay.portal.kernel.util.HtmlUtil" %>

<%@ page import="com.liferay.portal.kernel.bean.BeanParamUtil"%>

<%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%>

<%@page import="com.liferay.portlet.PortletURLFactoryUtil"%>

<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>

<%@ page import="com.liferay.portal.kernel.util.Validator"%>

<%@ page import="com.liferay.portal.kernel.util.LocaleUtil"%>

<%@ page import="com.liferay.portal.kernel.util.UnicodeFormatter"%>

<%@ page import="com.liferay.portal.kernel.util.Constants"%>

<%@ page import="com.liferay.portal.service.*" %>

<%@ page import="com.liferay.portal.model.*" %>

 

 

<%@

page import="javax.portlet.MimeResponse" %><%@

page import="javax.portlet.PortletConfig" %><%@

page import="javax.portlet.PortletContext" %><%@

page import="javax.portlet.PortletException" %><%@

page import="javax.portlet.PortletMode" %><%@

page import="javax.portlet.PortletPreferences" %><%@

page import="javax.portlet.PortletRequest" %><%@

page import="javax.portlet.PortletResponse" %><%@

page import="javax.portlet.PortletURL" %><%@

page import="javax.portlet.ResourceURL" %><%@

page import="javax.portlet.UnavailableException" %><%@

page import="javax.portlet.ValidatorException" %><%@

page import="javax.portlet.WindowState" %><%@

page import="java.util.*" %><%@

page import="java.text.*" %><%@

page contentType="text/html; charset=UTF-8" %><%@

page import="java.net.URLEncoder" %>

 

<portlet:defineObjects />

 

<portlet:actionURL name="addStatusEntry" var="addStatusEntryURL" />

 

<aui:form method="POST" action="javascript:void();" name="updateStatus" id="updateStatus">

           

        <aui:input type="textarea" name="content" placeholder="What are you working on?" cssClass="status-content"></aui:input>

                       

        <div class="clearfix">

            <a class="btn btn-primary pull-right" onClick="updateStatusForm();" id='<portlet:namespace/>stausSubmit'>Post</a>

        </div>

       

</aui:form>

 

<aui:script use="aui-io-request,aui-node">

    Liferay.provide(window,'updateStatusForm',

         function() {

          var A = AUI();

          A.io.request('<%=addStatusEntryURL%>',{

              method: 'POST',

              form: { id: '<portlet:namespace/>updateStatus' },

              on: {

                  success: function(){

                      ///Here code after sucess

                   }

             }

        });

  });

 

  </aui:script>

 

Below java action code:

 

/**

 *

 */

package tariqliferay.blogspot.com;

 

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.util.ParamUtil;

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

 

/**

 * @author tariqliferay.blogspot.com

 *

 */

public class AjaxFormSubmit extends MVCPortlet {

   

    @Override

    public void doView(RenderRequest renderRequest,

            RenderResponse renderResponse) throws IOException, PortletException {

        // TODO Auto-generated method stub

        super.doView(renderRequest, renderResponse);

    }

   

    @Override

    public void render(RenderRequest request, RenderResponse response)

            throws PortletException, IOException {

        // TODO Auto-generated method stub

        super.render(request, response);

    }

   

    @ProcessAction(name="addStatusEntry")

    public void addStatusEntry(ActionRequest actionRequest,

            ActionResponse actionResponse) throws IOException, PortletException {

        // TODO Auto-generated method stub

        String content = ParamUtil.getString(actionRequest, "content");

        System.out.println(content);

       

    }

 

}

Reference Blog:

http://tariqliferay.blogspot.com/2015/03/ajax-form-submit-in-liferay.html

 

Tariqul Islam
Posts: 2
Stars: 0
Date: 11/5/15
Test User EUSIA
Posts: 1
Stars: 0
Date: 1/6/15
Mahmudur Rahman Manna
Posts: 1
Stars: 0
Date: 12/16/14