Menu

Liferay 7: How to create a new portlet?


There were many interesting features added or improved in Liferay 7. And Liferay Workspace is one of them, it is a new way to organizing our source code and recommended by Liferay. In this post, I will create a portlet by Liferay 7 default style ( it means using Liferay Workspace).
(Phiên bản Tiếng Việt - Vietnamese version)

Environment installation

Liferay 7's tools

Firstly, let's download Liferay 7 Portal and install the development environment. And take a look the different between Liferay 7 and previous. Here are the tools and its links:
  • Liferay Portal 7.0.2 GA3: Click here to download.
  • Liferay IDE 3.1.0 M1: Let's download the suit version (Windows x64, Linux x64 or other versions at here). Mote: you can download only Liferay IDE and then plug in your Eclipse IDE. But, using the Liferay IDE Eclipse which is provided by Liferay is the best.

Configurations

Unzip two downloaded files and change some configurations.

Database configuration

The database configuration is similar to Liferay 6.x's one (there is the best tutorial). You have to create a file named portal-ext.properties in {liferay-portal-home}/tomcat-8.0.32/webapps/ROOT/WEB-INF/classes/ folder. And you must provide some configuration like this:

I am using PostgreSQL database in my example, I created a database name lportal7 in the local machine. If your database is different with mine, let's see the tutorial in this link. Note, Liferay 7 CE version in only supported the open source database like MySQL, PostgreSQL, ... (click here to reach the details).

Eclipse and Liferay portal configuration.

Create new Liferay Server: Open Eclipse IDE which has been unzipping. On the menu, click on New > Server > Server link below figure:

Click on Next button and select Liferay, Inc. >  Liferay 7.x,  see the next figure:


Click on Next button to move the next step:
At this step, enter the name of the server in name text box and browse to the Liferay Portal Bundle folder (which have been unzipping). Next, you must select the suit JRE runtime version (JRE 1.8 version is used in this example). Then, click on Finish button to get the end of configuration.
Last, you have to turn the created server on. After starting up, let's open a browser and go the http://localhost:8080 page. Amazing, your portal is here, you got it!

    Create new portlet

    After the setting up, as a Liferay developer, I will create a new project by Liferay 7 style. So, how do I create it?
    On the menu of Eclipse IDE, click on File > New > Liferay Module Project. A new popup window appears and you have to fill some information about your project like the below figure:
    You have to enter the project name, select a build tool and project template.There were two build tools (gradle and maven). You must select a fit project template (there were many project templates, I will introduce about them in the future posts.). For the purpose of introduction, I have choose mv-portlet template and click on Next button to reach the next step.

    In step, you have to enter the main class and package's name. And, lest click on Finish button to finish this creation. Now, you created a new project and the below figure is its structure.
    The structure of project which creates by gradle or maven tool is same. Java code and the resources separated. It is easy to code management
    Let's take a look, you do not see some familiar file like portlet.xml, liferay-portlet.xml và liferay-display.xml. Where are they?
    Next, we will review the content of TestPortlet class,

    As you can see, this class contains a new part (line 12th to 24th). Take a closer look, they are the pairs of key and value which were defined in some configuration file like portlet.xml, liferay-portlet.xml và liferay-display.xml. So, Liferay 7 instead of writing them on configuration files, write into Java code.

    Portlet deployment

    Next step is the deployment created project into the server. It is the easy task. You only right click on Liferay 7x server and chose "Add or Remove...." in float menu. Let's choose the portlet which will be deployed and click on OK button. The deployment will be complete automatically.
    Open your browser and drag your portlet on your page. Let's click on "+" button on the top right browser:
    Opening the "Sample" category and drag your portlet into any position. And then, your portlet will be displayed.

    Conclusion

    There is a simple Liferay 7 tutorial, it is for the Liferay beginner. And you can get my example project at here.

    Không có nhận xét nào:

    Đăng nhận xét