Liferay 7 Portlet Tutorial

In this tutorial, we will look into  Liferay 7 Portlet module creation along with OSGI configuration.  The  prerequisite for this tutorial is Liferay 7 Development setup  before creating module in Liferay DXP, so  Please click on this link to setup the development environment in eclipse.

Lets jump into creation of sample OSGI portlet module and will drag on to Liferay page.

Liferay7 Portlet example:

  • Liferay new module wizard:  Click File-> New -> Liferay Module Project

    Liferay module wizard

    Liferay module wizard

  • Select Module configuration:
    •   Liferay7 provides  “Project Template Name”  to create a modules. You can quicky create service builder, JSP fragment hook, MVC Portlet etc.  Here we select  module type as “MVC Portlet”. Select “mvcPortlet” and click on Finish.

      Liferay7 Project template

      Liferay7 Project template

  • We just created a portlet module with name “product” and folder structure will look like below:
    • All web resources in Liferay 7 are created  under src/main/resources folder but where as  Liferay 6 creates in docroot/html folder
    • Portlet configuration such as portlet.xml and liferay-portlet.xml configuration need to updated in @Component annotation under property attribute
    • The rest of Portlet API sush doView, processAction and servceResource all are same and you can directly use the API in JSP’s and Portlet Controller

      @Component

    • all portlet configuration goes into @Component  annotation that used to deploy in  OSGI container
      • immediate –  module will be deployed and should be started immediately instead of being lazy-loaded
      • property –    All java portlet properties should be  prefixed by: javax.portlet                                                                                                      Liferay properties are  prefixed by: com.liferay.portlet.   
      •  portlet.xml and liferay-portlet.xml   OSGI mappings :   you can find list of mappings on liferay site : https://dev.liferay.com/develop/reference/-/knowledge_base/7-0/portlet-descriptor-to-osgi-service-property-map
        @Component(
                immediate = true,
                property = {
               "com.liferay.portlet.display-category=category.sample",
               "com.liferay.portlet.instanceable=true",
               "javax.portlet.display-name=product Portlet",
                "javax.portlet.init-param.template-path=/",
                "javax.portlet.init-param.view-template=/view.jsp",
                "javax.portlet.resource-bundle=content.Language",
                "javax.portlet.security-role-ref=power-user,user"
                 },
               service = Portlet.class
        )

        Liferay 7 Portlet tutorial and gradle task

        Liferay 7 Portlet tutorial and gradle task

  • Liferay provides bnd.bnd and build.gradle files to manage build and deployment process
    • bnd.bnd  :   bnd file is MANIFEST configuration template file which used to auto create MANIFEST.MF,  so developers are not required to focus on MANIFEST file creation
      • It also used to activate Bundle with BundleActivator configuration if we are not going to use OSGI Declarative Services.
      • Bundle-SymbolicName: product 
        Bundle-Version: 1.0.0

 

  •  build.gradle:  build.gradle  is used for dependency and build management for Liferay 7 modules.
    • Liferay 7 provides Gradle Build Task panel  to execute  build,deploy, clean, buildService task
    • The generated build artifacts can be copied to deploy or  /bundles/osgi/modules folder for manual deployment
    • For auto deployment, make sure that tomcat is created under liferay-workspace/bundle folder
Liferay Gradle build task

Liferay Gradle Tasks

click on “deploy” target and you can see the logs that module is stated. The rest of portlet development is same as Liferay 6.

Now logon into Liferay portlet with test@liferay.com and add the portlet to page.  that’s all .. you have developed Liferay7 Portlet.

 

1 Comment

  1. PH

    A very nice material. Thanks for sharing your knowlege.

    Reply

Leave a Reply to PH Cancel reply

Your email address will not be published. Required fields are marked *