Creating a Bean Portlet

Your first step in developing a Bean Portlet is to create one. Here you’ll generate a Bean Portlet project and deploy your Bean Portlet to Liferay DXP.

  1. Generate a Bean Portlet project using a Maven command like this:

    mvn archetype:generate \
    -DarchetypeGroupId=com.liferay \
    -DarchetypeArtifactId=com.liferay.project.templates.cdi.bean.portlet \
    -DarchetypeVersion=1.0.0 \
    -DgroupId=com.mycompany \

    Here’s the resulting folder structure for a Bean Portlet class named Foo:

    • com.mycompany.demo.bean.portlet → Arbitrary project name.
      • src/main/java/

        • com.mycompany.constants.FooPortletKeys → Declares portlet constants.
        • com.mycompany.portlet.FooPortlet → Bean Portlet class.
      • src/main/webapp/WEB-INF/

        • jsp/view.jsp → Default view template.
        • beans.xml → Signals CDI to scan the portlet for annotations.
      • pom.xml → Specifies the project’s dependencies and packaging.

    Here’s the example Bean Portlet class:

    package com.mycompany.portlet;
    import com.mycompany.constants.FooPortletKeys;
    import com.liferay.bean.portlet.LiferayPortletConfiguration;
    import javax.inject.Inject;
    import javax.portlet.PortletConfig;
    import javax.portlet.annotations.LocaleString;
    import javax.portlet.annotations.PortletConfiguration;
    import javax.portlet.annotations.RenderMethod;
    	portletName = FooPortletKeys.Foo,
    	title = @LocaleString(value = FooPortletKeys.Foo))
    	portletName = FooPortletKeys.Foo,
    	properties = {
    public class FooPortlet {
    	PortletConfig portletConfig;
    		include = "/WEB-INF/jsp/view.jsp",
    		portletNames = {FooPortletKeys.Foo})
    	public String doView() {
    		return "Hello from " + portletConfig.getPortletName();
  2. Set any portlet configuration or Liferay portlet configuration values using @PortletConfiguration and Liferay@PortletConfiguration attributes.

  3. Inject any CDI beans using the @Inject annotation.

  4. Update your render method doView (it’s annotated with @RenderMethod). It displays the template WEB-INF/jsp/view.jsp by default.

  5. Add any other logic you like to your portlet class.

  6. Build your portlet:

    mvn clean package
  7. Deploy your portlet by copying the portlet WAR to your [Liferay Home]/deploy folder. The WAB Generator converts the WAR to an OSGi Web Application Bundle (WAB) and installs it to Liferay’s OSGi container.

Liferay DXP logs the deployment.

INFO  [main][HotDeployImpl:226] Deploying com.mycompany.demo.bean.portlet from queue
INFO  [main][PluginPackageUtil:1001] Reading plugin package for com.mycompany.demo.bean.portlet
INFO  [main][PortletHotDeployListener:181] 1 bean portlets for com.mycompany.demo.bean.portlet are available for use

The Bean Portlet is now available in the Liferay DXP UI. The example portlet is in the Widget category you assigned it.

Figure 1: The Foo portlet prints the message returned from doView method and shows the included JSPs contents.

Figure 1: The Foo portlet prints the message returned from `doView` method and shows the included JSP's contents.

Congratulations on creating and deploying a Bean Portlet!

OSGi CDI Integration

« Bean PortletIntroduction to Service Builder »