Home » Core Java » Liferay 7.2 MVC Portlet Development and Deployment

About Starblaster

Experienced Software Engineer with a demonstrated history of working in the information technology and services industry. Strong engineering professional skilled in Java, J2EE, Hibernate, Servlets, Spring Framework, Microservices, currently working on Liferay CMS.

Liferay 7.2 MVC Portlet Development and Deployment

In this post, we feature a comprehensive article about Liferay 7.2 MVC Portlet, an open source content management system that is developed in Java. It provides web content management and many inbuilt modules like login, web content, calendar, polls, users and roles management. Integration with LDAP. In Liferay, we can develop any dynamic website fastly. Liferay is also providing staging and comes with servers like tomcat, Jboss and many more so its very powerful CMS.

Liferay supports spring, OSGi, Struts, JSF and many other java frameworks. It also supports front end technology like react and angular base module development.

In this post, we feature a comprehensive article about the Liferay 7.2 MVC Portlet Development and Deployment. I am going to create a Liferay MVC portlet and deploy it into the Liferay server. Liferay MVC Portlet is using the Liferay MVC framework for MVC portlet application.

1. Project setup

Download eclipse and install Liferay7 ide plugin in eclipse.

Eclipse: click here

Steps to Liferay ide installation in eclipse: click here

Download Liferay 7.2  community server click here

Configured Liferay 7.2 as a new server in eclipse.

2. Create MVC Portlet

After installation of ide in eclipse open File > new > Liferay module project:

Liferay 7.2 MVC - new module project
new module project

Enter project name FirstMVC select build tool gradle or any other. Select Liferay version and select the project template name as mvc-portlet.

Liferay 7.2 MVC - project name
module project name

Enter component class name FirstMVC :

Liferay 7.2 MVC - class name and package
class name and package name

Package name com.org.javacodegeeks and then finish.

After finishing the portlet default structure is ready as per the following image:

Liferay 7.2 MVC - portlet folder
portlet folder structure

3. Deploy portlet in Liferay and then add more functionality inside

Open gradle tasks in eclipse click on deploy, it will generate jar file at FirstMVC\build\libs\com.org.javacodegeeks-1.0.0.jar which is our portlet jar.

To deploy portlet right-click on the Liferay server which is configured in eclipse.

Liferay 7.2 MVC - add and remove
add and remove portlet for deployment

Than add module as below than finish.

Liferay 7.2 MVC - FirstMVC added
FirstMVC added

We are able to see the server console with the message: started com.org.javacodegeeks-1.0.0.jar.

Liferay 7.2 MVC - server console
Liferay server console

Now go to a browser and log in with power user in Liferay and click on Add and search FirstMVC portlet .

Liferay 7.2 MVC - home page
Liferay home page

Now add portlet on to page.you will see a message like “Hello from FirstMVC!”

Liferay 7.2 MVC - FirstMVC Portlet
FirstMVC Portlet added on page

Notes: During deployment, if you are facing an issue like below:

Error:PWC6345: There is an error in invoking javac.

Solution: 

In eclipse click on windows > preferences > installed JRE > edit JRE and give path

(Like C:\Program Files\Java\jdk1.8.0_231) up to JDK.

Right Click on FirstMVC Module properties > java build path > libraries >Edit JRE and select alternate JRE apply and close

Redeploy FirstMVC again.

Another way to deploy portlet is just copy generated jar file from FirstMVC\build\libs\com.org.javacodegeeks-1.0.0.jar and paste in to deploy folder of Liferay (liferay-ce-portal-7.2.0-ga1\deploy ).

Portlet structure files explanation: FirstMVCPortlet.java fileFirstMVCPortlet.java

@Component(
	immediate = true,
	property = {
		"com.liferay.portlet.display-category=category.sample",
		"com.liferay.portlet.header-portlet-css=/css/main.css",
		"com.liferay.portlet.instanceable=true",
		"javax.portlet.display-name=FirstMVC",
		"javax.portlet.init-param.template-path=/",
		"javax.portlet.init-param.view-template=/view.jsp",
		"javax.portlet.name=" + FirstMVCPortletKeys.FIRSTMVC,
		"javax.portlet.resource-bundle=content.Language",
		"javax.portlet.security-role-ref=power-user,user"
	},
	service = Portlet.class
)

"com.liferay.portlet.display-category=category.sample" is the category where the portlet will show up.

"com.liferay.portlet.header-portlet-css=/css/main.css" is the CSS file to use in the portlet.

"com.liferay.portlet.instanceable=true" means we are able to add portlet more than one time on the same page.

"javax.portlet.display-name=FirstMVC" as the name suggest portlet display name

"javax.portlet.init-param.view-template=/view.jsp" first jsp to be rendered when we add the portlet

"javax.portlet.name=" + FirstMVCPortletKeys.FIRSTMVC is the portlet id which is unique

"javax.portlet.resource-bundle=content.Language"this property is used to specify Language.properties

"javax.portlet.security-role-ref=power-user,user" roles for users who can access the portlet

Bnd.bnd file

Bundle-Name: FirstMVC > bundled name

Bundle-SymbolicName: com.org.javacodegeeks > name which is used to generate portlet jar

Bundle-Version: 1.0.0 > version of module

Export-Package: com.org.javacodegeeks.constants: export package path so can available publicly

Now let’s add some extra stuff:

Open init.jsp

And add below code if it is not present:init.jsp

 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ 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/portlet" prefix="liferay-portlet"%>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>

Open view.jsp and add the below code:

In code, we have created a simple Html form to submit user detail.we have to use an action URL(provided by Liferay for post request) for form action value.we have taken two Html text boxes. when we submit the form controller class FirstMVCPortlet addUser Method will invoke. specify name = “addUser” while creating an action URL it will use to identify which method of FirstMVCPortlet class needs to call. Then create a portlet:actionURL with var=”addUserURL” and name=”addUser” than use as a below form action value addUserURL using jsp expression tag in below html form.

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

Add User

<form action=”” name=”userForm” method=”POST”> User Name
<input type=”text” name=”userName” id=”userName” />
PhoneNumber
<input type=”text” name=”userPhoneNumber” id=”userPhoneNumber” />
</form
FirstMVCPortlet Java file with below code.In addUser method we are getting request paramter throgh actionRequest like userName and userPhoneNumber. we will set this value in hashmap object we will pass this hashmap object to userDetails.jsp to display submited values.to pass the values to another jsp we are setting attribute in actionRequest.FirstMVCPortlet.java
package com.org.javacodegeeks.portlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.Portlet;
import javax.portlet.PortletException;
import org.osgi.service.component.annotations.Component;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import com.liferay.portal.kernel.util.ParamUtil;
import com.org.javacodegeeks.constants.FirstMVCPortletKeys;
/**
 * @author STARBLASTER
 */
@Component(immediate = true, property = { "com.liferay.portlet.display-category=category.sample",
		"com.liferay.portlet.header-portlet-css=/css/main.css", "com.liferay.portlet.instanceable=true",
		"javax.portlet.display-name=FirstMVC", "javax.portlet.init-param.template-path=/",
		"javax.portlet.init-param.view-template=/view.jsp", "javax.portlet.name=" + FirstMVCPortletKeys.FIRSTMVC,
		"javax.portlet.resource-bundle=content.Language",
		"javax.portlet.security-role-ref=power-user,user" }, service = Portlet.class)
public class FirstMVCPortlet extends MVCPortlet {

	public void addUser(ActionRequest actionRequest, ActionResponse actionResponse)
			throws IOException, PortletException {
		String userName = ParamUtil.getString(actionRequest, "userName");
		String userPhoneNumber = ParamUtil.getString(actionRequest, "userPhoneNumber");
		Map userMap = new HashMap();
		userMap.put("userName", userName);
		userMap.put("userPhoneNumber", userPhoneNumber);
		actionRequest.setAttribute("userMap", userMap);
		actionResponse.setRenderParameter("mvcPath", "/META-INF/resources/usersDetalil.jsp");
	}
}
Create jsp file in META-INF/resources folder userDetail.jsp and add below codeuserDetail.jsp
<%@page import="java.util.Map"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

than after above imports add next line <portlet:defineObjects/> and then add below code.In code we are getting value of hashmap object that we are already passing form our controller FirstMVCPortlet addUser Method.get the values from userMap object with key userName and userPhonenumber.userDetail.jsp

 Users
<%
Map userMap=(Map)renderRequest.getAttribute("userMap");
if(userMap!=null){
%>
Name: <%=userMap.get("userName")%>
PhoneNumber: <%=userMap.get("userPhoneNumber")%> <%}%>

Now redeploy portlet and our user submit form is created submit the form and view user detail.

We have successfully created liferay mvc portlet.

Liferay 7.2 MVC - Add user form
Add user form

4. Download the Source Code

This was example of Liferay MVC Portlet.

Download
You can download the full source code of this example here: Liferay 7.2 MVC Portlet Development and Deployment

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments