24 June, 2014

Using Portlet Session in Liferay

Here is the sample example using login form that explains how to use PortletSession in liferay. In general PortletSession has default scope limited to Portlet which can be extended to APPLICATION_SCOPE by changing property.

Let us understand it using Login example.



1: Login.jsp

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

<portlet:defineObjects />

<portlet:actionURL var="editActionURL" name="editAction">
</portlet:actionURL>

<portlet:actionURL var="logoutActionURL" name="logoutAction">
</portlet:actionURL>

This is the <b>SampleClass</b> portlet in View mode.
<%
    PortletSession pSession = null;
    if(renderRequest.getPortletSession()!=null)
    {
        pSession  = renderRequest.getPortletSession();
    }
    if(pSession== null || pSession.getAttribute("loginSuccess")==null)
    {   
%>
<aui:form action="<%=editActionURL %>">
    <aui:input type="text" name="txtUserName" id="txtUserName" label="UserName:"/>
    <aui:input type="password" name="txtPassword" id="txtPassword" label="Password:"/>
   
    <aui:button-row>
        <aui:button type="submit" value="submit" id="btnSubmit"/>
        <aui:button type="button" value="Cancel" id="btnCancel"/>
    </aui:button-row>
</aui:form>

<aui:script>
    AUI().use('aui-base',function(A){
        A.one('<portlet:namespace/>btnCancel').on('click',function()
        {
            A.one('<portlet:namespace/>txtUserName').val('');
            A.one('<portlet:namespace/>txtPassword').val('');
        });
    });
</aui:script>

<%
}
else
{
%>
    User Already Logged in !!!
    <aui:button name="btnLogout" id="btnLogout" value="Logout" href="<%=logoutActionURL %>">
    </aui:button>
<%       
}
%>

Here we are writing username and password, by clicking on Submit Button Action class will be called and username password will get verified. On Successful verification we create a PortletSession Variable and initialize it with value in either case we keep it null.

2:Java code Below:

import com.liferay.util.bridges.mvc.MVCPortlet;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletSession;

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

    public void editAction(ActionRequest aActionRequest,ActionResponse aActionResponse)
            throws IOException,PortletException
            {
                String usrName = aActionRequest.getParameter("txtUserName").toString();
                String pass = aActionRequest.getParameter("txtPassword").toString();
                PortletSession session  = aActionRequest.getPortletSession();
             
                if(usrName.equals("jay") && pass.equals("jay"))
                {
                    session.setAttribute("loginSuccess", "true");
                }
                else
                {
                    session.setAttribute("loginSuccess", null);
                }
            }
   
    public void logoutAction(ActionRequest aActionRequest, ActionResponse aActionResponse)
            throws IOException,PortletException
            {
                PortletSession session = aActionRequest.getPortletSession();
                session.setAttribute("loginSuccess", null);
            }
}

'loginSuccess' variable will be initialized from action class and will accordingly be checked on JSP page. If user is already login then Show Logout Button. This Logic is included on JSP Page

Popular Posts

Featured Post

Liferay 7.3 compatibility matrix

Compatibility Matrix Liferay's general policy is to test Liferay Portal CE against newer major releases of operating systems, open s...