Showing posts with label portlet. Show all posts
Showing posts with label portlet. Show all posts

Tuesday, 29 August 2017

Create MVC Portlet in Liferay 7 using LiferayIDE

Create MVC Portlet in Liferay 7 using LiferayIDE

Liferay 7 has adopted OSGI standards.In OSGI everything we create is a module.For more information on OSGI visit my previous blog onOSGI.



Create MVC Module using Liferay IDE.


GoTo New→File click on Liferay Module Project.


  • Enter Project Name and select mvcportlet as project template.
  • Click Next and enter component class name and package name then click on finished.




Generated module will be inside module folder of liferay workspace.

Generated Portlet Class

  • Based on input provided generated portlet class is 


ProductRegistrationPortlet.java


package com.liferay.product.portlet;


import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;


import javax.portlet.Portlet;


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


@Component(

immediate = true,
property = {
"com.liferay.portlet.display-category=category.sample",
"com.liferay.portlet.instanceable=true",
"javax.portlet.display-name=ProductRegistration 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 ProductRegistrationPortlet extends MVCPortlet {

}

  • Here property{

          
         }
tag is used to specify properties that are previously specified in portlet.xml and  liferay-portlet.xml

  • For more detail on mapping of portlet XML descriptor values to OSGi service properties click here.
  • Now we can have to build and deploy generated mvc portlet using gradle tasks as shown in fig.



  • Click on build this will generate jar file of porltet module.On clicking deploy this jar will be deployed and your portlet will be available.
  • Note you must have to set liferay.workspace.home.dirand liferay.workspace.modules.default.repository.enabled     properties in gradle.properties (for more information click here)

CRUD Operations in Liferay 7

CRUD Operations in Liferay 7


Following are some simple step to execute CRUD operation in LiferayDXP.

Step1 Create mvc portlet module with name ProductRegistration and Component class name ProductRegistrationPortlet and package name com.liferay.product.portlet.
For more information read my previous blog on creating mvc module portlet 



Step 2 Create Service Builder Module with name product-registration-service and package name com.liferay.product.service. and run build service gradle task. For more information on this visit my previous blog on creating service builder module

Step 3 Open build.gradle file of ProductRegistration module and add gradle dependencies of the api module generated after building service.

compileOnly project(":modules:product-registration-service:product-registration-service-api")

Step 4 Refresh your gradle project using ctrl+f5 so that above dependencies get resolved.

Step 5 Create addProduct.jsp in /META-INF/resources/ folder of your MVC portlet.

Step 6 Create renderURL in view.jsp as shown below.

view.jsp

<%@ include file="init.jsp" %>
<portlet:renderURL var="addProductURL">
<portlet:param name="mvcPath" value="/addProduct.jsp"/>
</portlet:renderURL>


<aui:button onClick="${addProductURL}" value="add-product"></aui:button>

Step 7 Create aui-form for user input in  addProduct.jsp as shown below

<%@ include file="init.jsp"%>
<portlet:actionURL name="addProduct" var="addProductURL">
</portlet:actionURL>

<aui:form action="${addProductURL}">
<aui:input name="productName" label="productName"></aui:input>
<aui:input name="productPrice" label="productPrice"></aui:input>
<aui:input name="" type="submit" value="add-product"></aui:input>
</aui:form>

Step 8 Create render method and process action method with name addProduct to scan user input in your controller as shown below.

package com.liferay.product.portlet;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.product.service.service.ProductLocalServiceUtil;

import java.io.IOException;

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

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

@Component(
immediate = true,
property = {
"com.liferay.portlet.display-category=category.sample",
"com.liferay.portlet.instanceable=true",
"javax.portlet.display-name=ProductRegistration 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 ProductRegistrationPortlet extends MVCPortlet {
private static final Log log=LogFactoryUtil.getLog(ProductRegistrationPortlet.class);
@Override
public void render(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
// TODO Auto-generated method stub
super.render(renderRequest, renderResponse);
}
@ProcessAction(name="addProduct")
public void addProduct(ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException {

}
}

Step 9 Create a saveProduct method in ProductLocalServiceImpl located in service module inside package com.liferay.product.service.service.impl as shown below

package com.liferay.product.service.service.impl;
import aQute.bnd.annotation.ProviderType;
public class ProductLocalServiceImpl extends ProductLocalServiceBaseImpl {

public void saveProduct(String productName,long productPrice){

Product product=new ProductImpl();
product.setProductId(counterLocalService.increment());
product.setProductName(productName);
product.setProductPrice(productPrice);
updateProduct(product);

}
}

Step 10 Goto gradle task execute following task.

  • For product-registration-service-service and perform
               build service
               build 
               deploy
  • For product-registration-service-api
              build
              deploy

Then refresh your gradle project to resolve dependencies 

Step 10 Scan user input and saveProduct method in controller as shown below.

package com.liferay.product.portlet;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.product.service.service.ProductLocalServiceUtil;

import java.io.IOException;

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

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

@Component(
immediate = true,
property = {
"com.liferay.portlet.display-category=category.sample",
"com.liferay.portlet.instanceable=true",
"javax.portlet.display-name=ProductRegistration 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 ProductRegistrationPortlet extends MVCPortlet {
private static final Log log=LogFactoryUtil.getLog(ProductRegistrationPortlet.class);
@Override
public void render(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
// TODO Auto-generated method stub
super.render(renderRequest, renderResponse);
}
@ProcessAction(name="addProduct")
public void addProduct(ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException {
String productName=ParamUtil.getString(actionRequest,"productName");
long productPrice=ParamUtil.getLong(actionRequest,"productPrice");
ProductLocalServiceUtil.saveProduct(productName, productPrice);
}
}

We are all done build and deploy ProductRegistration MVC Module then add portlet on any page .

MyOutput

Final Project Structure

Summary in this post we see how to add record in database using Liferay 7.
In my future post we will learn how to fetch all records from database and display them on jsp using SearchContainer in liferay 7.

In case of any query please let me know.