Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
Lior Hadaya
Extending MVCPortlet
May 23, 2012 5:15 AM
Answer

Lior Hadaya

Rank: Regular Member

Posts: 138

Join Date: January 24, 2012

Recent Posts

Hi,

I need to create sort of a "BasePortlet" class, that will extend Liferay's MVCPortlet.
Did anyone ever do that before? What is the best practice for that?

I thought of creating a simple java project, with one class (base portlet), but I'm not quite sure how to add LIferay's dependencies correctly.
I'm wondering whether I need to use a Liferay project and not just a simple java project, but then what type should I choose? It's neither a portlet or a hook or.. etc..

Any help would be highly appreciated.
Thanks!
Olivier Bonnet-Torrès
RE: Extending MVCPortlet
May 23, 2012 5:19 AM
Answer

Olivier Bonnet-Torrès

Rank: Regular Member

Posts: 148

Join Date: July 11, 2008

Recent Posts

Hi!
If you want your base portlet to be reusable, it may be viewed as an ext plugin extending the util-bridges part of the portal.
You should use Liferay project for plugins in order to retrieve dependencies (much less tedious than configuring a plain Java project).
Lior Hadaya
RE: Extending MVCPortlet
May 23, 2012 7:12 AM
Answer

Lior Hadaya

Rank: Regular Member

Posts: 138

Join Date: January 24, 2012

Recent Posts

Thank you for your quick response!

I just want to make my intentions clear, I don't want to change the implementation of the MVCPortlet.
I want to create another class, that will extend MVCPortlet, and I could use it only when I create a portlet who's <portlet-class> is my new Base Portlet class.
I don't want my new implementation to affect all the Liferay's out of the box portlets. I only want to use it when I choose to.

Should I do this using an extension plugin? or is there any other (better) way? I saw that using ext-plugin is not very recommended and should be used only when there's no other choice.

Thanks!
Lior Hadaya
RE: Extending MVCPortlet
May 27, 2012 10:41 PM
Answer

Lior Hadaya

Rank: Regular Member

Posts: 138

Join Date: January 24, 2012

Recent Posts

Hi,
Does anyone have any idea how to approach this issue? I really need some advice.
Thanks
David H Nebinger
RE: Extending MVCPortlet
May 29, 2012 6:31 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 6233

Join Date: September 1, 2006

Recent Posts

It's quite easy to do. I'd actually use an EXT plugin for the base class as that would make it available to all of your other portlets.

Create the extension class in the EXT plugin. Then when you're creating your own plugin portlets, use the regular MVC type then change portlet.xml to refer to your class in the EXT plugin instead.
Lior Hadaya
RE: Extending MVCPortlet
May 29, 2012 7:08 AM
Answer

Lior Hadaya

Rank: Regular Member

Posts: 138

Join Date: January 24, 2012

Recent Posts

David, thanks for replying.

I'm sorry but since I'm new to Liferay I haven't been able to understand your solution.
If I create a base class using the EXT plugin that would essentially replace the MVCPortlet implementation, doesn't that mean that any change I make in this base class would affect all of Liferay's OOTB portlets which I don't want to do?
Is there any way to achieve my goal without using the EXT plugin?

Create the extension class in the EXT plugin. Then when you're creating your own plugin portlets, use the regular MVC type then change portlet.xml to refer to your class in the EXT plugin instead.

By "use the regular MVC type" do you mean inherit from it? what would I need to change in portlet.xml?

Thanks again,
Lior
David H Nebinger
RE: Extending MVCPortlet
May 29, 2012 9:05 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 6233

Join Date: September 1, 2006

Recent Posts

Well, let's separate the concerns and deal w/ them individually...

First, I'm not talking about replacing MVCPortlet, just extending it. You'd define your extension such as follows:

1package com.example;
2
3import com.liferay.util.bridges.mvc.MVCPortlet;
4
5class MyMVCPortlet extends MVCPortlet {
6  // your extension stuff goes here
7}


That will give you your extension class where you can add or override methods.

To use the class, you need to edit portlet.xml to change the <portlet-class /> tag to be:

1<portlet-class>com.example.MyMVCPortlet</portlet-class>


That will give you your extension class and bind it within your portlet so your instance is used rather than the normal MVCPortlet class.

The second concern is how to re-use your extension class in multiple portlet projects. You have two options here:

  1. Copy the com.example.MyMVCPortlet class into every new portlet project you create.
  2. Use the EXT plugin to share the com.example.MyMVCPortlet class.


The second option would be my preferred path as you are only maintaining one copy of MyMVCPortlet, and any code changes you make can be included on every redeployment...
Vitor Silva
RE: Extending MVCPortlet
May 29, 2012 9:59 AM
Answer

Vitor Silva

LIFERAY STAFF

Rank: Junior Member

Posts: 36

Join Date: May 3, 2012

Recent Posts

It would depend in what sort of logic you would want to have in your class. If it is a generic class you can use the following aproaches:

1 - Copy the class to every portlet project that you have,
2 - Have only one war file with all of the portlets inside and the class there as well
3 - Create an ext project and install it on liferay.
4 - Create a java project with the class and add the project as a dependency to every portlet project that you have. This will replicate the jar.
I have implemented solutions 2 and 4, but I guess it would depend on how you want to manage it.
David H Nebinger
RE: Extending MVCPortlet
May 29, 2012 10:29 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 6233

Join Date: September 1, 2006

Recent Posts

Vitor Silva:
3 - Create an ext project and install it on liferay.
4 - Create a java project with the class and add the project as a dependency to every portlet project that you have. This will replicate the jar.
I have implemented solutions 2 and 4, but I guess it would depend on how you want to manage it.


If you've done #4, it's a quick step towards completing #3. Create an EXT plugin and include the jar file there, then all of your portlets can declare it as a dependent jar and it will get pulled in automatically on it's own at deployment time.

#4 on it's own is not much better than copying the class file around because it is just as easy to get out of sync.

#3 has a side effect though, if you deploy EXT w/ the jar, then a portlet that pulls in the jar, then an update to the jar in the EXT plugin, the dependent portlets are not automatically updated - you have to re-deploy the portlet so the new dependent jar is copied in.
Lior Hadaya
RE: Extending MVCPortlet
May 30, 2012 7:10 AM
Answer

Lior Hadaya

Rank: Regular Member

Posts: 138

Join Date: January 24, 2012

Recent Posts

Thank you both very much.
Right now I think we will avoid implementing this until we have a clear requirement that would force us to do so.
We wanted to write this base class with no special implementation and have other developers inherit from it so that in the future we could add code that would affect everyone, but since it involves an ext plugin I think it would be better for us to wait for such a requirement, and hope it never comes..