Saturday, 17 May 2025

Creating Custom Portlets with Liferay DXP + Java.

Creating Custom Portlets with Liferay DXP + Java

Liferay DXP (Digital Experience Platform) provides a modular framework for developing robust web applications using Java. In this blog, we will walk through the steps required to create a custom portlet using Liferay DXP with Java, covering even the smallest configuration and setup details.

Prerequisites

  • JDK 11 or later
  • Liferay DXP 7.4 (or any compatible version)
  • Blade CLI installed
  • IDE like IntelliJ IDEA or Eclipse

Step 1: Set up Liferay Workspace

Open terminal and run the following command:

blade init liferay-workspace

Navigate to your workspace:

cd liferay-workspace

Step 2: Create a Portlet Module

blade create -t mvc-portlet -p com.example.portlet -c CustomPortlet custom-portlet

This command creates a new portlet module using the MVC Portlet template.

Step 3: Explore the Generated Files

  • CustomPortlet.java - The main class for the portlet.
  • view.jsp - Default view template for the portlet.
  • bnd.bnd - Contains OSGi metadata.

Step 4: Update view.jsp

Edit src/main/resources/META-INF/resources/view.jsp to customize the UI:

<%@ page contentType="text/html;charset=UTF-8" %>
<h1>Welcome to My Custom Portlet</h1>
<p>This is a custom portlet built with Java.</p>

Step 5: Build and Deploy the Portlet

./gradlew deploy

This will deploy your portlet to the Liferay DXP instance.

Step 6: Add the Portlet to a Page

  1. Login to the Liferay portal.
  2. Navigate to a site page or create a new one.
  3. Click on the "+" button to add a widget.
  4. Search for your portlet (e.g., "Custom Portlet").
  5. Drag and drop it onto the page.

Step 7: Customize Further (Optional)

You can create additional JSPs and actions, or use Spring MVC for more complex logic. You can also define PortletPreferences, add localization files, and implement inter-portlet communication using public render parameters.

Conclusion

You've now successfully created a custom portlet with Liferay DXP using Java. From setting up the workspace to deploying the module, each step builds your foundation for more advanced Liferay development.

No comments:

Post a Comment