Blog Post


Liferay Spring MVC Portlet (+JPA, Maven) part.1

Recently we’ve received a new request by our client to implement a Liferay Portlet that uses JPA as a primary ORM-system. Sure, Liferay provides Service Builders that enable ORM functionality in portlets, but I was always wondering why some companies prefer to develop their own frameworks instead of using already existing good solutions. So in this task we had to move from Liferay ORM + Liferay Transactions to well known and mature frameworks: Java JPA + Spring.

First of all we need to clarify some terms:

Servlet – is a Java implementation to support HTTP request/response protocol. So every servlet includes some code to handle client requests (POST/GET/…) and usually maintained by a servlet container (Tomcat, JBoss,…).

Portlets – are pluggable managed my portal web applications that define user interface look. These can be observed as small applications (email, user management, calendar) combined in a single portal view. Portals like servlets handle requests/responses, but in contrast to servlets portlets support only 3 request types: action, resource, render.

Spring Web MVC – is the implementation of Model-View-Controller pattern, build around Spring’s DispatcherServlet, where Controller is a Java Old Plain Object (POJO) that maps request urls to corresponding methods. The concept of Spring MVC is the same as in Servlets (handle HTTP requests), and Servlets can be substituted by Spring MVC providing easier setup and reducing development time. POJO Controller example:

public class HelloWorldController {

    public String helloWorld(Model model) {
        model.addAttribute("message", "Hello World!");
        return "helloWorld";

Spring Portlet MVC - developed to support Portlet specification (JSR-168), the concepts of it are the same as in Spring Web MVC with differences created by the unique workflow of portlets.

The main way in which portlet workflow differs from servlet workflow is that the request to the portlet can have two distinct phases: the action phase and the render phase. The action phase is executed only once and is where any 'backend' changes or actions occur, such as making changes in a database. The render phase then produces what is displayed to the user each time the display is refreshed.

Like in ordinary portlets, the DispatcherPortlet should be declared in the portlet.xml of the web application:

        <title>Sample Portlet</title>


The rendering in Portlet MVC is an extension of Spring Web MVC that reuses all view abilities of Spring. This rendering converts Portlet Requests/Responses to HttpServletRequests/Responses and then calls the render() method of the View interface. To make this work, DispatcherPortlet should use a ViewRendererServlet which must be declared in web.xml. Here is the final web.xml file for given application that will have only 2 entries: 



As we can see from the upper snippet – ViewRendererServlet mapped to /WEB-INF/servlet/view URL. One more thing we should add to use Spring view rendering mechanism is to define a PortletModeHandlerMapping, which will bind incoming requests based on the current mode of the portlet (“view”, “edit”, “help”). Here is spring context file snippet:

<bean id="portletModeHandlerMapping"
    <property name="portletModeMap">
            <entry key="view">
                <ref bean="requestController" />

Request controller for Portlet MVC must implement *.portlet.mvc.Controller interface and appropriate to Portlet workflow methods handleActionRequest(), handleRenderRequest():

public interface Controller {

    ModelAndView handleRenderRequest(RenderRequest request, RenderResponse response)
        throws Exception;

    void handleActionRequest(ActionRequest request, ActionResponse response)
        throws Exception;


As mentioned before, Portlet MVC uses all Spring view functions, that allows us to inject ViewResolver implementation to current portlet. There are different out-of-the-box ViewResolver can be used (JSP, Freemarker, Velocity), we will use JSP resolver that is also defined in spring context file:

<bean id="viewResolver"
    <property name="viewClass"
        value="org.springframework.web.servlet.view.InternalResourceView" />
    <property name="prefix" value="/" />
    <property name="suffix" value=".jsp" />

In order not to mess up the Portlet configuration with the Maven definitions I put maven dependency list to the end of this article:

        <!-- Spring -->

Actually you will need spring-tx and spring-orm libraries for JPA implementation only, so you can omit these 2 dependencies.

At this point our Liferay Spring Portlet is ready. In next article I’ll show how to inject JPA to this portlet.

Comments (11)

Tags: liferay



Credo Systemz Mar 01, 2017 at 05:50 #

great blog to read... thanks a lot for sharing


blue prism Training Aug 21, 2018 at 15:51 #

This is a great article. It gave me a lot of useful information. I use it everyday to check my ranking and keep me focused on what I need to do. Thank you for making the honest effort to discuss this. I feel very strong about it and want to learn more. It could be extremely useful and helpful for me and my friends.


KISHORE Nov 21, 2018 at 11:40 #

quite informative, thanks for sharing with us.Thanks for posting this useful content, Good to know about new things here, Your post is really awesome. Your blog is really helpful for me to develop my skills in a right way. Thanks for sharing this unique information with us.


Saranraj Nov 28, 2018 at 13:11 #

Thanks for this kind of worthy information. this was really very helpful to me. keep continuing.


Sam Daniel Jan 28, 2019 at 08:59 #

Wonderful Blog. Keep Posting.


Advanced Excel Training Jan 28, 2019 at 08:59 #

Keep Blogging


Shanthi Feb 16, 2019 at 06:04 #

Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live


rijaz May 07, 2019 at 12:34 #

Hey, would you mind if I share your blog with my twitter group


Selenium training in Chennai Aug 28, 2019 at 08:50 #

Very informative and nice article


krish Feb 03, 2020 at 11:05 #

nice article


trishana training institute Mar 01, 2020 at 06:45 #

thank you for sharing this blog, it is very helpful information and keep posting.....

Leave a Comment