Tuesday 11 October 2022

Read DB data in fragment

Read DB data in fragment

In this blog we will see how to fetch the DB table data into the fragment .will go with the example step by step. For better understanding I have created one sample module [ employee ]in IDE.


Step 1 – Create sample module [ with service builder module as well ].

I have created module with name - Employee

In the module “employee” have created JSP to enter data which got saved in “Employee” table.

Build and deploy this modules in local and do some data entry for “Employee” table .

service.xml for employee service builder module 




Step 2 – Create fragment in the site.






I have created fragment with name – “show employee data”


Paste the below code in the fragment


<!-- Service locator to find the employeeLocalService -->
[#assign empLocalService = serviceLocator.findService("com.employee.db.service.EmployeeLocalService")]
<!-- Will fetch All the data -->
[#assign empList=empLocalService.getEmployees(-1, -1)]
<!-- Will fetch the parameter from browser URL ex- /emp?empId=101 - will set 101 to empId-->
[#assign empId=paramUtil.getLong(themeDisplay.getRequest(), "empId")]
[#if empId != 0]
[#attempt]
<!-- Fetching employee data for particular employeeId [ empId ]-->
[#assign empObj = empLocalService.getEmployee(empId)]
<h3>
<b>
<!-- Display returned data in Bold -->
${empObj.getEmployeeName()} -- ${empObj.getEmployeeDepartment()}
</b>
</h3>
[#recover]
<h3>

<!-- Show this when no matching data found -->
Failed to get emp data ! !
</h3>
[/#attempt]
[/#if]
[#if empList?has_content]


<!-- Loop over all the employee data and show all data -->
[#list empList as employeeObj]
<h5>
${employeeObj.getEmployeeName()} -- ${employeeObj.getEmployeeDepartment()}
</h5>
[/#list]
[/#if]




Step 3 – Check the result on Web Page by passing the id in browser URL .
Ex. http://localhost:8080/web/infinity/home?empId=206403

Sunday 24 April 2022

How to create Login hook on Liferay DXP 7.3

 [1] Create a new "Liferay Module Project" with project template "mvc-portlet" as shown in below image.








[2] remove the JSP files as we not need it for this hook. change the controller as below shown in the image .



That's it for the POST Login Hook in liferay 7.3 . 
You can find the various liferay events on below link.

Liferay Event List