Developing Your First Portlet
Step 1 of 8
Now you’ll learn step-by-step how to create your project and deploy your application to Liferay DXP. Before you know it, you’ll have your application deployed alongside those that come with Liferay DXP.
Your first application is simple: you’ll build a guestbook application that looks like this:
By default, it shows guestbook messages that users leave on your website. To add a message, you click the Add Entry button to show a form for entering and saving a message.
Ready to write your first Liferay DXP application?
Creating Your First Liferay DXP Application
Your first step is to create a Liferay Module Project. Modules are the core building blocks of Liferay DXP applications. Every application is made from one or more modules. Each module encapsulates a functional piece of an application. Multiple modules form a complete application.
These modules are OSGi modules. The OSGi container in Liferay DXP can run any OSGi module. Each module is packaged as a JAR file that contains a manifest file. The manifest is needed for the container to recognize the module. Technically, a module that contains only a manifest is still valid. Of course, such a module wouldn’t be very interesting.
Now you’ll create your first module. For the purpose of this Learning Path, you’ll create your modules inside your Liferay Workspace. Follow these instructions to create your first Liferay Module Project:
In the Project Explorer in Liferay Dev Studio DXP, right click on your Liferay Workspace and select New → Liferay Module Project.
Complete the first screen of the wizard with the following information:
guestbook-webfor the Project name.
- Use the Gradle Build type.
mvc-portletfor the Project Template.
On the second screen of the wizard, enter
Guestbookfor the component class name, and
com.liferay.docs.guestbook.portletfor the package name. Click Finish.
Note that it may take a while for Dev Studio DXP to create your project, because Gradle
downloads your project’s dependencies for you during project creation. Once this
is done, you have a module project named
template configured the project with the proper dependencies and generated all
the files you need to get started:
- The portlet class (in the package you specified)
- JSP files (in
- Language properties (also in
Your new module project is a portlet application. Next, you’ll learn exactly what a portlet is.
What is a Portlet?
Web applications can be simple: they might show you one piece of information, such as an article. A complex application might track your taxes as you enter lots of data into an application that calculates whether you owe or are due a refund. These applications run on a platform that provides application developers the building blocks they need to make applications.
Liferay DXP provides a platform that contains common features needed by today’s applications, including user management, security, user interfaces, services, and more. Portlets are one of those basic building blocks. Often a web application takes up the entire page. If you want, you can do this with applications in Liferay DXP as well. Portlets, however, let you serve many applications on the same page at the same time. Liferay DXP’s framework takes this into account at every step. For example, features like platform-generated URLs exist to support Liferay’s ability to serve multiple applications on the same page.
What is a Component?
Portlets created in Liferay Module Projects are generated as Components. If a module (sometimes also called a bundle) encapsulates pieces of your application, a component is the object that contains the core functionality. A Component is managed by a component framework or container. Components are deployed inside modules, and they’re created, started, stopped, and destroyed as needed by the container. What a perfect model for a web application! It can be made available only when needed, and when it’s not, the container can make sure it doesn’t use resources needed by other components.
In this case, you created a Declarative Services (DS) component. With Declarative Services, you declare that an object is a component, and you define data about the component so the container knows how to manage it. A default configuration was created for you; you’ll examine it later.
Deploying the Application
Even though all you’ve done is generate it, the
guestbook-web project is ready
to be built and deployed.
Make sure that your server is running, and if it isn’t, select it in Dev Studio DXP’s Servers pane and click the start button ().
After it starts, drag and drop the
guestbook-webproject from the Project Explorer to the server.
Open a browser and navigate to Liferay DXP (http://localhost:8080 by default).
If this is your first time starting Liferay DXP, you’ll go through a short wizard to set up your server. In this wizard, make sure you use the default database (Hypersonic). Although this database isn’t intended for production use, it works fine for development and testing.
To add an application to a page, click Add () in the upper right hand corner.
Select Widgets. In the Applications list, your application should appear in the Sample category. Its name is
Now you’re ready to jump in and start developing your Guestbook portlet.