Liferay is an open source Java based portal server. It has built-in support for common portal features like user/organization management, site management, workflow based content management, document management and is extensible via plugins.

 This blog will focus on developing Spring MVC portlet plugins using Eclipse/Liferay IDE and Maven. Spring MVC portlet has many advantages over other web frameworks, including the default Liferay MVC framework for developing portlets –

  •  It doesn’t hide the portlet lifecycle like other web frameworks
  • It has support for dependency injection and POJO based development (Liferay MVC does not support dependency injection)
  • Mirror image of Spring web servlet MVC and thus familiar to a large section of the Spring/Java community
  • Provides mock objects for unit testing

Eclipse setup

Install Eclipse 4.4.0 EE luna on your desktop. Setup Liferay IDE as mentioned in http://www.liferay.com/community/wiki/-/wiki/Main/Liferay+IDE+Installation+Guide

Create a new Liferay plugin project in eclipse and choose the plugin type as portlet. Choose build-type as Maven. Click Next and choose Liferay MVC as the portlet framework (we will modify this setting to use Spring MVC portlet framework later). The following directory structure will be created –

  • portlet-plugin/
    • src/
      • main/
        • java/
        • resources/
        • webapp/
          • css/
            • main.css
          • js/
            • main.js
          • WEB-INF/
            • liferay-display.xml
            • liferay-plugin-package.properties
            • liferay-portlet.xml
            • portlet.xml
            • web.xml
          • icon.png
          • view.jsp
    • pom.xml

portletflow

XML configuration

Make the following changes in portlet.xml

Make the following changes in web.xml for the spring handler to resolve the view –

Add the following spring jars as dependencies in your pom.xml – spring-asm, spring-beans, spring-context, spring-core, spring-expression, spring-webmvc-portlet, spring-webmvc, spring-context-support, spring-web. Use version as 3.0.7-RELEASE

Create a file <portletname>-portlet.xml under WEB-INF and place the following code in the file

If you want some other name and/or place the file somewhere else, then pass it as a config parameter to DispatcherPortlet as show above in portlet.xml.

The component-scan scans your handler class and registers it as a request handler.

The dispatcher portlet

  • Sends requests to the appropriate handler
  • Resolves the view using ViewResolver
  • Dispatches requests to the ViewRendererServlet to render views

The view resolver resolves logical view names to an actual jsp file (you can use other formats like velocity as well). Once the view is resolved, the the Dispatcher portlet dispatches the request to the ViewRendererServlet.

Modify liferay-portlet.xml to add the following tag –

Handler implementation

Create your spring handler class. Add @Controller and @RequestMapping annotations to map urls to this handler class. One handler is mapped to one portlet mode.

Add action and render methods in the handler class. They should annotated with @ActionMapping and @RenderMapping annotations respectively. In these methods, you can get the request parameters and do your processing. Return the next view in the render methods. These view strings are resolved by view resolver spring class into a particular jsp.

Example action method –

userName will be mapped to ‘userName’ request parameter.

Example render method –

The view resolver will resolve the above entry into renderOne.jsp and render that jsp file.

In addition to request parameters, you can setup model attributes in your handler. Methods annotated with @ModelAttribute are executed before the action and render methods of the portlet handler.

To obtain model attributes in your model, annotate your action method parameter method as shown above.

To map request parameters to the model, use @ModelAttribute at the method parameter level. The request parameters are automatically mapped by spring to this command object (use a JavaBean class to map all request parameters to the model. In the example above, the ModelUserName class should have a userName property).

JSP code

Obtain your render parameters and render the jsp. <portlet:defineObjects /> makes available the render parameters using renderRequest implicit variable.

Example –

Once you create the above source and configuration files, deploy the project into Liferay and test your code