MVC Portlet with Liferay DXP (OSGi module)

MVC Portlet with Liferay DXP (OSGi module)





MVC Portlet with Liferay DXP (OSGi module)

Liferay MVC portlet is light weight implementation of GenericPortlet class of JSR-168 standards . It also provides MVCActionCommand, MVCRenderCommand, MVCResourceCommand base classes to segregate logic of doProcess, doView, serveResource in their individual classes. These improves the modularity and code re-usability .

This is a basic sample of Hello world portlet . This portlet contains one input field in a form for the user to enter the name and that entered value is then being displayed.

Use Liferay developer studio 3 to create the Liferay MVC Portlet. Below are the detailed steps of the process.

Steps to create Liferay MVC Portlet (In OSGi Module).

1). Steps to Create Liferay Module Project .

 Select Project Template Name as mvcportlet only and click next




Specify the Class name and package details along with properties.



Click on Finish.

2) Module Project Structure

This structure is generated by Liferay Developer Studio





3) Override doView and processAction method of generated Helloworldmvcportlet class.
doView -for rendering
processAction – for performing the required action - process


package com.cd.learning.portlet;

import com.cd.learning.service.PaymentService;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import com.liferay.portal.kernel.util.ParamUtil;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component(immediate = true, property = { "com.liferay.portlet.display-category=category.CDLiferayDXP",
  "com.liferay.portlet.instanceable=true", "javax.portlet.display-name=HelloWorld Portlet",
  "javax.portlet.init-param.template-path=/", "javax.portlet.init-param.view-template=/view.jsp",
  "javax.portlet.resource-bundle=content.Language",
  "javax.portlet.security-role-ref=power-user,user" }, service = Portlet.class)
public class HelloworldmvcportletPortlet extends MVCPortlet {

 @Override
 public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
   throws IOException, PortletException {
  System.out.println("Hey you are in DoView Method of HelloworldmvcportletPortlet ...!!");
  super.doView(renderRequest, renderResponse);
 }

 @Override
 public void processAction(ActionRequest actionRequest, ActionResponse actionResponse)
   throws IOException, PortletException {
  System.out.println("Hey you are in Process Action Method of HelloworldmvcportletPortlet ...!!");
  String name = ParamUtil.getString(actionRequest, "name", "Mr. X");
  System.out.println("Name :: " + name);
  actionRequest.setAttribute("name", name);
  super.processAction(actionRequest, actionResponse);
 }
}

4)Update the view.jsp to hold the input field. The same jsp is being used to display the entered value. 

<%@ include file="/init.jsp" %>

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

<portlet:defineObjects />

This is the <b>HelloWorld</b> 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://liferay.com/tld/aui" prefix="aui" %>

<portlet:defineObjects />

<liferay-theme:defineObjects />
<%
 String name = (String) request.getAttribute("name");
 if(null!=name && name.length() > 0) { %>
  Welcome <%= name %> 
 <% }
%>
<br/><br/><br/><br/>
<portlet:actionURL var="helloWorldURL"/>

<aui:form action="<%= helloWorldURL %>"  method="post">
 <aui:input name="<portlet:namespace/>name" /> <br/>
 <aui:button type="submit" value="Submit" />
</aui:form>
 
5) Build & Deploy the portlet  

Using gradle tool we can deploy HelloWorld module in osgi. This is screen of gradle tool.



























  • Click on deploy. This will build and deploy the portlet.
  • Below is the gradle console log when we issue deploy command.






Console log.
 
From above log we are sure that our module project is successfully deployed in osgi module.
It will generate jar file inside in bundles – at the path: bundles\osgi\modules  of the server

6) Add the portlet on the page
Log-in to the portal using admin
Add portlet from application sample category.

















Enter User Name in above screen.