How to create multiple portlets in single Liferay plugin project

Liferay Plugin project can be created in Liferay IDE. We can add multiple portlets in single liferay plugin project. They all combined in single WAR file.
So far we have seen that whenever we need to create a portlet, we are creating one plugin project. It’s possible to have multiple plugins (like portlets,hooks, layouts and theme) in single Liferay plugin project. 

When we build such plugin project, all plugins will be part of single WAR file. But when it deployed , then each plugins (porltet,hooks,theme and layouts) will be deployed as separate folder under <<Liferay Bundle>>/tomcat/webapps folder.


Let us see how to create multiple plugins ( Portlets, Hooks, Layouts, Theme) in single Liferay plugin project in eclipse.

I have taken an example of creating Liferay MVC portlet. Please refer my previous blog on How to create Liferay MVC Portlet


Give project name as multiple-plugin and -portlet  will be appended by eclipse IDE so that the final project name will be multiple-plugin-portlet as per below screenshot.

How to create multiple portlets in single Liferay plugin project - first-project-structure

Important Note :- Don’t confuse with the Plugin project and portlet. Let me differentiate them.

  • When we want to create any Liferay component ( like portlet, hook, theme etc) we need to first create Liferay Plugin project.
  • When we create Liferay plugin project from eclipse,( by clicking on File–>New–>Liferay Project ) it will give option (like portlet, hook, layout, theme or ext) as below screenshot.

How to create multiple portlets in single Liferay plugin project - plugin-type

  • If we choose Portlet then it will create Liferay plugin project with by portlet (which have same name as project we gave in this wizard).
  • If we choose different plugin type then it will create Liferay plugin project by that selected component.
  • In short, When we create Liferay plugin project, it will create the component of selected plugin type with same name as plugin project.
  • So you can consider Liferay plugin project as container where all liferay components resides as per below screenshot.
How to create multiple portlets in single Liferay plugin project - Plugin-project-structure

Explanation:-

  • Outer side section (blue border) with cloud shape is the Liferay Plugin project.
  • Each rectangle withing it is showing various components (Liferay Plugins)
  • The Yellow rectangle (with named Default Component) shows the default component. This component type will be same as we have selected while creating plugin project.
  • In short, the default component will be portlet, if plugin type portlet is selected while creating plugin project. It will be hook it plugin type Hook is selected while creating plugin project etc.
Important Notes:- 
  • Liferay Plugin project (for plugin type portlet ) will only accommodate plugins of type Portlets, Hooks or Layouts. Theme and ext can’t be mix with portlets and Hooks in same plugin project. 
  • if you want to create Theme or ext plugin, you need to create separate plugin project.

Add multiple portlets

Now I assume that you have clear this concept !!!!!  🙂 Let us experience this with the plugin project(multiple-plugin-portlet) we have just created. If you have selected plugin type as portlet  then the default component for this plugin project is portlet.
 
Open portlet.xml file. It will looks like below screenshot.
How to create multiple portlets in single Liferay plugin project - default-component
 
You can observe that it will have one portlet element entry. Also the portlet have the same name as plugin project. If you open liferay-portlet.xml file, then it will looks like below screenshot
How to create multiple portlets in single Liferay plugin project - liferay-portlet
You also can observe that, its having one portlet entry, having portlet name same as plugin  project name.
 
Now let us see how to add more portlet. To add portlet in existing plugin project, click on File–>New–>Liferay Portlet (instead of Liferay Project. Selecting Liferay Project will create new plugin project) If will show below screen
How to create multiple portlets in single Liferay plugin project - adding-new-portlet
  • This time, it will first ask in which plugin project do we want to add new portlet ?
  • Its showing drop down and we choose it multiple-plugin-portlet 
  • Give the appropriate value for Portlet class, Java Package ans super Class. 
  • I gave the value as shown in above screenshot.
  • Now click on Finish button
You will observe that in portlet.xml and liferay-portlet.xml file, new entry for portlet ‘portlet2‘ is added as per below screenshot.
 
For portlet.xml file
How to create multiple portlets in single Liferay plugin project - new-entry-for-portlet.xml
 
For liferay-portlet.xml file
 
How to create multiple portlets in single Liferay plugin project - new-entry-for-liferay-portlet.xml
 
Similarly you can add more type. Only one thing you need to make sure if plugin type is selected as portlet while creating plugin project, then other component of type Hook and Layout template only can be added.
 
Keep try adding more portlets / Hooks / Layout templates and experience how its works.
You can observe that after building, you will have only one WAR file but when you deploy ti liferay server, there will be separate folder for each component type under webapps folder.
 
You can refer this link for additional information about liferay plugin project. Feel free to ask any question. I will try my best to get the answer.

I would recommend looking at index page ‘A Complete Liferay Guide‘ to browse all topics about liferay.

Share This Post

35 Comments - Write a Comment

  1. Hi

    We have a scenario, where we need to write multiple hooks for same Liferay Action (Eg: Login post & pre events), but we need to configure each hook for different site. Let’s say, I have two hooks “Hook A” & “Hook B”, I need to configure “Hook A for site A” & “Hook B for Site B”. I figured how we can do it for JSP’s using “application adapter hooks”, but I need similar control on Liferay actions. Can you please direct me in right way if you have already come across this scenario or with any web resources and I want to know, whether it is possible or not. Thanks in adavance

    Reply
    1. Hello Sai Srinivas,

      Sorry for late respond. I haven’t come across such scenario out of the box. However, for each site, you can have custom attribute and based on the request come to your Action in hook, you find the current site and based on that take the appropriate logic. Hope this works for you.

      Feel free to ask questions / give suggestions.

      Regards
      Nilang

      Reply
  2. hi,
    i need to add comments particular lines of the documents uploaded in the wiki portlet.multiple users should be able to view and comment simultaneously on this particular section.how can manipulate the wiki potlet to do so

    Reply
    1. Hi Asha

      Welcome to Techblog. What I understand from your comment is you want to extends out of the box functionality. In case if you wish to change the existing liferay code, you need to either create hook or EXT. Use hook if you wish to change label, struts action, override existing service method, customizing JSP. Use EXT plugin if you wish to change IMPL class or any uderlying java class.

      Let me know if this works for you.

      Regards
      Nilang

      Reply
  3. Hi nilang,
    I need seperate service.xml files for each portlet. Is this possible for single plug-in project?
    Also i need to use different for different service.xml files.
    I tried using tag but it’s not allowing different …..
    “Exception:There can only be one namespace element” it’s showing.
    What i need is to get different namespaces infront of entities of single schema like sample_Foo and sample1_Foo where sample and sample1 are namespaces in different service files. Is this possible?
    Pls Help me….

    Reply
    1. Hi Praneeth,

      In case if you want to have different service.xml for each portlet, then they should be in different plugin project. for Single plugin project, there will be just one service.xml file. You can define different entity in service.xml file.

      If you wan to have different namespace, then create different service.xml file and give separate namespace for that. In case if you want to share same namespace, you can still give it for different service.xml file.

      Let me know if this solve your issue.

      Regards
      Nilang

      Reply
    2. Hi Praneeth,

      Appreciated your answer. I feel, I have not fully understand your answer. Can you please elaborate more on this ? What was you are looking for any how you get by table attribute ?

      Regards
      Nilang

      Reply
    1. Hi,
      Welcome to Tech blog and thanks for writing here. You need to perform IPC (Inter portlet communication). I am writing separate blog on the same. So you can join face book page or become member so that once new blog added, you will get an email for the same.

      Regards
      Nilang

      Reply
  4. Hello,
    i want to create a plugin that would allow me to choose from a list of keywords that a crawler uses to filter.
    Specifically:
    1) i want to have in the left panel (left side of window), a list of words which the program will read from a configuration file
    2) when i click on a word, the program has to insert that word in the right panel (right side of window)

    I have created 2 portlets in a single Liferay plugin project. One portlet for the left side and one portlet for the right one. When I insert a word from left portlet, the right panel has to be updated and the word must be there. I cannot understand how to do this action and specifically how the right portlet can understand that an action has been down
    in the left portlet and it must be updated.I’ve only managed to update the portlet correctly,but it is shown in the left panel and not at the right where it should be.Could you help me please? (I use render and action request for jsp communication)

    Thanks in advance for your time.

    Reply
    1. Hi Kostas,

      Welcome to Tech blog. I understand your requirement in which you want to trigger some action on second portlet when something happens on first portlet. Liferay provide IPC (Inter Portlet communication) mechanism for that. This can be achieve in liferay by following 3 ways
      1) Session
      2) Render Parameter
      3) Event.

      The last way (Event) is recommended. it is basically work as publlisher – listener pattern and loosely coupled between two portlets. I am planning to write separate blog on IPC in Liferay. Till that time you can google “IPC in liferay” and get some idea.

      Feel free to ask questions / give suggestion and don’t forget to join this tech blog via social media.

      Regards
      Nilang

      Reply
  5. Hey how to add multiple spring MVC portlets in one project … When i write two different xml files it shows error stating classnotfoundexception in the second xml ..

    Reply
  6. Thank you soo much nilang, It is soo helpful for us. Please try to keep more concepts on Liferay. I am looking forward for your posts. Good Way. All the Best.

    Reply
  7. Hi Nilang,

    first of all many thanks for your various blog posts, they very helpful!!

    Srping Version 3.2.2
    Liferay 6.2 CE
    Eclipse IDE 1.6.2

    The issue I’m having is the following:

    I have 3 portlets in one .war file, when I deploy the war I can see them as separate ones as you mentioned. What happens is, when I add all of them to a page they all display correctly, but as soon I invoke the action phase by submitting a form, does not matter on which portlets form, the page displays an error message for each portlet, saying ” is temporarily unavailable.”

    What I saw from my log is that all the portlets controller get invoked in some way, and I come up with three error saying that the .jsp file could not be found.
    In particular, for the portlet on which I tried to submit the form, the error says that could not found the jsp that supposed to print the results after an action, and for the other two, their defaultRender.jsp.

    What I think I understand is that, since all the portlets reside on the same page, when I submit something on that same page, their render phase is invoked, but then I can’t see why that error given that the jsps file are previously correctly rendered.

    Hope you can help me out with this.

    Many thanks in advance,
    Ettore.

    Reply
    1. Hi Ettore,

      First of all Welcome to Techblog !!!

      In case if more than one portlets are there in same WAR file then just make sure to put the jsp files under folder for each portlet. This way it will be easily manage jsps for individual portlets.

      The reason I am assuming in your case is that it is not finidng the correct jsp file.

      If its possible, can you please share the code so that I can check in more detail.

      Regards
      Nilang

      Reply
  8. Hi Nilangs,

    I have used your example of Spring MVC portlet. How we can created multiple portlet in single spring mvc protlet project. How we can do component scan? so that request can differentiate the controller.

    Thanks,
    Ajay

    Reply
    1. Hi Ajay,

      You can create multiple portlets in single liferay plugin project for Spring MVC portlet also. Following are the steps
      1)Create separate entry for each portlet in portlet.xml file (portlet-name will be different but the portlet-class will be same)
      2)create separate applicationcontext file for each portlet and put the reference of it in corresponding portlet.xml file with init-para
      3)Put the controller and view resolver entry in each application context file.

      You can put the component scan for each these application context file (each for separate portlet).

      Hope this will work. Let me know if you need further help.

      Regards
      Nilang

      Reply
    2. Dear Nilang,

      Thanks for quick response. I am doing the same thing. But issue is that how i can share the same database session factory in all the portlets in this project?
      Do i need to configure session factory in all the application context file?

      Thanks,
      Ajay

      Reply
    3. Hi Ajay,

      Try following steps. I assume that you have created more than 2 Spring MVC Portlet in single portlet plugin project.
      1) Create META-INF folder just under src folder
      2) create ext-sprint.xml file under it
      3) add following data in it.

      <?xml version=”1.0″ encoding=”UTF-8″?>

      <beans
      default-destroy-method=”destroy”
      default-init-method=”afterPropertiesSet”
      xmlns=”http://www.springframework.org/schema/beans”
      xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
      xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      >

      and you can define your Session factory, transaction manager, datasource etc in this file. (you you may need to add the namespce for required components)

      Once this done, try to access the bean defined in this xml file into your portlet specific application context. (You can do wiring to your controller)

      Try this way and let me know if this works or not. I haven’t tested it but this should work

      Reply
    4. Dear,
      Find the below code as
      Meta-INF/ext-spring.xml
      ————–

      WEB-INF/smtp.properties

      org.hibernate.dialect.SQLServerDialect

      update
      true
      false

      classpath:hibernate.cfg.xml

      com.pacnet.ext.connect.ext.entity

      ${smtp.auth}
      ${smtp.starttls.enable}

      Orders-portlet.xml
      —–

      communications-portlet.xml
      ———————————-

      Add these files have refered in the Init-Param of portlet configration in portlet.xml.

      After the changes you suggest it’s not able to resolve the orderService which define in ext-spring.xml

      Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.pacnet.ext.connect.ext.service.OrdersService com.pac
      net.connect.ext.orders.controller.OrderListControler.ordersService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionExceptio
      n: No matching bean of type [com.pacnet.ext.connect.ext.service.OrdersService] found for dependency: expected at least 1 bean which qualifies as autow
      ire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostP
      rocessor.java:506)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProc
      essor.java:284)
      … 47 more

      Reply
    5. Sorry I am not able to put the xml files here. Could you please share the email address so that i can send you. My mail id is [email protected]

      context:component-scan base-package=”com.pacnet.connect.ext.orders.controller”
      bean class=”org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping”
      bean class=”com.pacnet.connect.ext.orders.controller.OrderListControler”

      And below this i have resolver. And same thing i have for other xml

      Reply
    1. Hi Suyash,

      Thanks for writing ….!!
      The purpose of Ext is to extends existing classes / JSPs / configuraitons. Ideally there should be only one ext. You can put all your changes in that. Frankly telling, I haven’t tried to have multiple EXT. But its obvious not good practice.

      Regards
      Nilang I Patel

      Reply

Post Comment