Tuesday, 9 June 2015

Popup liferay

Hi
In this post I created a simple MVC portlet in which I will render a jsp in my popup. When this popup submitted then it hits action class and then return to first jsp and popup will disappear.

portlet.xml
    <portlet>
        <portlet-name>pop-up-jsp</portlet-name>
        <display-name>Pop Up Jsp</display-name>
        <portlet-class>com.pop.up.PopUpJSP</portlet-class>
        <init-param>
            <name>view-template</name>
            <value>/html/popupjsp/view.jsp</value>
        </init-param>

        <expiration-cache>0</expiration-cache>
        <supports>
            <mime-type>text/html</mime-type>
            <portlet-mode>view</portlet-mode>
        </supports>
        <portlet-info>
            <title>Pop Up Jsp</title>
            <short-title>Pop Up Jsp</short-title>
            <keywords></keywords>
        </portlet-info>
        <security-role-ref>
            <role-name>administrator</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>guest</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>power-user</role-name>
        </security-role-ref>
        <security-role-ref>
            <role-name>user</role-name>
        </security-role-ref>
    </portlet>

My jsp pages folder as mentioned in portlet.xml file
<init-param>
            <name>view-template</name>
            <value>/html/popupjsp/view.jsp</value>
</init-param>


view.jsp

<%@ page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@ page import="javax.portlet.PortletURL"%>
<%@ page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="theme" %>

<portlet:defineObjects />
<theme:defineObjects />
<h1>Popup portlet</h1>
<%
    PortletURL popupURL = renderResponse.createRenderURL();
    popupURL.setWindowState(LiferayWindowState.POP_UP);
    popupURL.setParameter("paramOne","valOne");
    popupURL.setParameter("paramTwo","valTwo");
    popupURL.setParameter("redirectURL",themeDisplay.getURLCurrent());
    popupURL.setParameter("jspPage", "/html/popupjsp/popup.jsp");
    String popUpFunction = "javascript:openPopup('"+popupURL.toString()+"');";
%>
<a href="<%= popUpFunction %>">Press here To see popup</a>

<script>
function openPopup(){
      var URL = '<%=popupURL%>';
       AUI().use('liferay-util-window', 'aui-io-deprecated', function(A) {
        modal=Liferay.Util.openWindow({
        dialog: {
        id:'discountpage',
        centered: true,
        modal: true,
        width: 550,
        height: 650,
        },
        uri: URL
        });
       
       });
      }                                        
</script>
Here I am creating a render url and setting some parameters that I can access in my popup jsp page.
I created one java script function which is called by href tag and this javascript function is calling render url and hence popup would appear.

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

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

<portlet:defineObjects />

<%
    String paramOne = renderRequest.getParameter("paramOne");
    String paramTwo = renderRequest.getParameter("paramTwo");
    String redirectURL = renderRequest.getParameter("redirectURL");
    out.println("paramone: "+paramOne);
    out.println("paramtwo: "+paramTwo);
    PortletURL actionMethodURL = renderResponse.createActionURL();
    actionMethodURL.setParameter(ActionRequest.ACTION_NAME, "redirectToViewJsp");
%>

<aui:form method="POST" action="<%= actionMethodURL.toString() %>" target="_parent">
    <aui:input name="redirectURL" type="hidden" value="<%=redirectURL %>" />
    <aui:input name="myName" type="text" />   
    <aui:button type="submit" value="Save" />
</aui:form>

Here I created one aui form and I set one hidden variable to send the value of redirectURL which we are getting from view.jsp page as this I will set in action class as sendRedirect method to send it to finally to view.jsp page.

Finally my action class

package com.pop.up;

import java.io.IOException;

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

import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

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

    public void redirectToViewJsp(ActionRequest actionRequest, ActionResponse actionResponse)
    throws IOException, PortletException {
        String redirectURL = ParamUtil.getString(actionRequest, "redirectURL");
        String myName = ParamUtil.getString(actionRequest, "myName");
        actionResponse.sendRedirect(redirectURL);
        System.out.println("redirectToViewJsp action class param value: : "+myName);
       
    }
}

In action class I am fetching two variables and then one I simply used to print purpose and other one to redirect to a desire jsp
Thanks

No comments:

Post a Comment