Hi All,
Greetings of the day!
Today we will see how we can perform CRUD operation in Liferay DXP with help of AJAX.
Below will be our output after complete development
So let's start.
Step 1)First of all we need to create an MVC module portlet.
So go to File-New-Liferay Module Project & select project template name as MVC-portlet and give project name as sports-management
Then click on next and provide component name and package name then click on finish.
If you are unable to create a portlet visit my below post which will provide complete steps for creating a portlet.
How To Create MVC Portlet
Step 2)Now we will create a service builder project which will be used for the service and repository layer of our project.
So go to File -New-Liferay Module Project & select project template name as service builder and add project name as sports-service
Then click on next & provide package name and then click on finish
Step 3)Define database table structure and other useful methods for CRUD operation in service.xml
So go to service.xml in sports-service and add the below code in service.xml
Here we have to define a sports table with 3 columns.
Step 4)Now we will execute the following command in sequence
- For sports-service
- For sports-API
If you don't know how to perform the above steps visit my below post for detailed steps for deploying the module project to tomcat.
Step 5)Now we will add service layer methods in sports-service
So first right-click on sport-service and go to Gradle then click on refresh-gradle-project
Then you will find multiple classes are autogenerated, we will be using SportLocalServiceImpl.java class and we will add methods for CRUD operation which we will be using in our portlet module through sport-API.
Now again repeat the below step
- For sports-service
- For sports-API
Once you build and deploy a sports service module using the above steps, the methods you have added will be available in SportLocalServiceUtil which we will be using in our portlet.
Note- We should not use SportLocalServiceImpl directly we should use methods of SportLocalServiceImpl through SportLocalServiceUtil which will be in our sports-API module
Step 6)Now we will give sports-service-api dependency in the sports-management portlet so we can use service layer methods in our controller.
So open build.xml and add the below dependency
compileOnly project(":modules:sports-service:sports-service-api")
Our service layer coding is done, now we will focus on the portlet module.
Step 7)So Open SportsManagementPortlet.java and add the below property in @Component
"com.liferay.portlet.requires-namespaced-parameters=false".
Now we will override the server resource method as we are performing crud operation using AJAX. We will retrieve the flag which we will pass from JSP page using jquery, and based on the flag we will call different service layer methods using SportLocalServiceUtil.
Step 8)Now let's add code for the JSP page, we will be using a single JSP file and we will be using jquery to send flags to our server resource method, along with form data.
First, let's add body parts for our JSP where we will add a basic form for adding/updating sports and a table structure for listing sports.
We will show and hide add sport form using jquery with the click of add sport and cancel button.
So now we will add jquery code for different event
- On click of add sport button, we will show add sport form and on click of cancel, we will clear data filled in the form. So let's add the jquery function
- On clicking of submit button of add/update sport form, we will call the server resource method with the flag add-update-sport and the data user has filled in the form. So let's define the jquery function for the same.
- Now we will add the jquery function to display a list of the sport we have added so far, here we will create a dynamic row and add those rows to the table we have added in the body, this function will be called on page load, on add-sport, on-update sport and on delete sport. So after each event, our table gets refreshed with fresh data
We can call above function using $.fn.loadSports();
Now we will add a jquery function which will be called with the click of the delete button to delete a record, this function will also call the server resource method with flag value as delete-sport.
- Now with the click of the edit button, we will populate add/update sport form with values of the row on which the user has clicked on edit. Note- here we are using the same form for add and update and based on id backend logic will decide to add a new sport or update the existing sport.
Below is the complete code of the view.jsp with all CSS & jquery for your reference.
Below is the final project structure for reference
Sport-management-mvc-portlet
Sport-management-service module
If you have any doubt let me know.
Happy Learning.
thanks for code..it really helpful for me!...
ReplyDelete