Asset Publisher

Expert Insights Hub

Stay connected with us to discover what top tech minds think about cutting-edge trends, development platforms, team hiring, automation, and innovations shaping the industry. Gain valuable knowledge from industry leaders, explore unique strategies, and stay ahead in the ever-evolving world of technology. Stay informed, stay inspired, and stay ahead of the competition with our exclusive updates!

Blogs

CRUD Operations Using MVC Portlet in Liferay 7.4

Caption

Introduction:

CRUD (Create, Read, Update, Delete) operations are fundamental for managing data in web applications, and Liferay's MVC Portlet offers a robust framework for implementing them efficiently. The MVC Portlet follows the Model-View-Controller design pattern, enabling developers to create modular and maintainable applications. In this blog, we’ll provide a step-by-step guide to implementing CRUD operations using an MVC Portlet in Liferay 7.4, showcasing how to seamlessly handle data creation, retrieval, updating, and deletion within the Liferay platform. 

Prerequisite:

  • JDK 11 
  • Liferay Server: liferay-dxp-tomcat-2024.q4.0 
  • Liferay Developer Studio:  3.9.7.202207131011-g

Steps to Perform CRUD Operations Using an MVC Portlet in Liferay 7.4: 

1. Start the Liferay Developer Studio:

  • Open Liferay Developer Studio
  • Start the Liferay server by navigating to the Servers tab and clicking on the Start button.      

2. Create a Workspace Project:

  • Open Liferay Developer Studio and start the server. 
  • Navigate to File → New → Liferay Workspace Project
  • Provide a project name (e.g., global-workspace) and click Finish.

3. Create a Service Builder:

  •  Go to File → New → Liferay Module Project
  • Choose the Service Builder template, provide the project name (e.g., employee-service), and click Finish
  • Enter the class and package name. 
  • Modify the service.xml file to define an entity (e.g. Employee-service) with attributes. Set the remote-service attribute to true.

4. Build the ServiceBuilder Module

  • Open Gradle Task Executor in Liferay Developer Studio. 
  • In the Gradle Task Executor, navigate to Modules. 
  • Go to the Employee Service module. 
  • Under the Employee Service module, go to the Build section. 
  • Click on Build Service to execute the build task.

  • Refresh the Gradle project to generate necessary packages and classes.


     

5. Implement business logic for (CRUD) Create, Read, Update, Delete in service builder. 

  • Create: Add logic to save a new entity (e.g., an employee) to the database.

  • Read: Fetch all employees.

  • Update: Modify an existing employee.

  • Delete: Remove an employee by employeeId.

6. Create a Portlet:

  • Navigate to File → New → Liferay Module Project
  • Provide a project name (e.g., employee-portlet) and click Finish
  • Select “Project Template Name” as “mvc-portlet” and Click on “Next” .
  • Enter the class and package name click on “Finish”. The necessary file structure for mvc module will gets created as below.

7. Integrate Service Builder with the Portlet

  • To add the dependency for employee-service in your MVC portlet, include the following line in the build.gradle file of your MVC portlet.


8. Now, create the addEmployee.jsp file in the same directory as view.jsp and add the following code to addEmployee.jsp.

Output of addEmployee.jsp

9. Now, implement the processAction method to add an employee in the controller class of the MVC portlet.


10. Now add below code in view.jsp



11. Now override render method of portlet to fetch all employees and send employees list to view.jsp

Output of view.jsp

12. Now, create the updateEmployee.jsp file in the same directory as view.jsp and add the following code to updateEmployee.jsp.



13. Now, implement the processAction methods for the update action.


Output of updateEmployee.jsp


14. Now, implement the processAction method for the delete action.

Output of delete action

If you have any query, click here.​​​​​​​​​​
Blogs