Total Pageviews

Tuesday, July 26, 2011

MVC portlet in Liferay 6

The Liferay 6 plugins SDK is fully re-architectured. The ext environment comes now as plugin, and therefore adding new fields(customization) the core liferay portlets of Liferay is now a bit difficult as it was quite easy in LR5. The normal way most of us are accustomed to creating a portlet was through the StrutsPortlet class which is Liferay Struts bridge. But we cannot extend this class in the plugins SDK as the StrutsPortlet class is packaged within the portal-impl.jar file, which is not accessible in the plugins SDK.


There is a new addition - MVCPortlet, which is Liferay MVC pattern class. When we create a portlet in the Plugins SDK,  we get an MVCPortlet. You can chek it by viewing the <portlet-class> attribute of portlet.xml file. Some of its major features are - page management, automatic display of success messages, automatically calling the appropriate action methods an so on. This class extends the LiferayPortlet class.  


To do some custom task by this MVC Portlet, we need to create a new class that must extend the MVCPortlet. So, the question is whay to extend MVCPortlet class? My answer is quite small - Extending MVCPortlet class will help us various ways, broadly we can say - we'll get about all benefits of MVCPortlet class and will add our own functionality in the class we are writing. 


Try creating with me by following the steps mentioned below - 


1. Create a new portlet by entering the command (must be in portlet folder of plugin)
create abc "abc"


NOTE: I suggest you to go and examine the xml files which are created by default.


2. Now go to '\portlets\abc-portlet\docroot\WEB-INF' and open 'portlet.xml' file. Modify the <portlet-class> attribute. The default valuve will be <portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class>. Give here the package of your class. we are giving 'com.xyz.action.ABCAction'.


3. In the <init-param> attribute, give the relative path of the jsp that you wish to open by default, when the portlet gets loded. For, instance we are giving <value>/html/com/xyz/view.jsp</value>.


4. Now create the class in the respective package. We writing the following ABCAction class in '\portlets\abc-portlet\docroot\WEB-INF\src\com\xyz\action'


5. Now just one more thing, create the jsp files in the respective path. here, we will create in '\portlets\abc-portlet\docroot\html\com\xyz' (don't bother to create the folder and files if u need, ypu'll need here to create html folder in docroot). Now the point to coin in mind that we must have the same name of the <portlet:actionURL> corresponding to which function we wish to invoke for our custom class. We here have the name of the actionURL and  function is 'customFunction'


init.jsp
view.jsp


This is all I've did. Hope, you'll find this article helpful.

6 comments:

  1. Hi Apoorva,

    It was very good article.
    Do you have full steps to create struts portlet in Liferay 6

    ReplyDelete
  2. Hi Himanshu,
    I'll leave a blog post for it soon.

    ReplyDelete
  3. is it possible to have more action classes in MVC portlet..

    ReplyDelete
  4. Hi Apoorva,
    This might be totally out of line, but i just thought i might ask you this.

    I want to retrieve a user profile picture. How do i do it? Could you please share a code snippet? Im using Liferay 6.0.6. It has only user.getPortraitId() and no user.getPortraitURL(). So once i get the portrait id inside a JAVA class, what do i do with it?

    ReplyDelete
    Replies
    1. Hi Praveen,

      Have a look on this post...

      http://www.apoorvaprakash.in/2012/05/getting-user-image.html

      Thanks,
      Apoorva

      Delete