Create maven Spring MVC Portlet in Liferay

Liferay provides various framework to create plugins. Spring MVC is one of the most popular framework to create portlets on Liferay.

In this article, I will be showing how to create Maven Spring MVC Portlet in liferay. Newer version of Liferay (6.2 & DXP) supports creating plugins with Maven.

Maven Spring MVC Portlet in liferay


Before starting further, I would recommend to visit my previous blog on how to configure maven, set maven profile and basic understanding about creating new liferay maven plugin.

If you are creating your liferay plugin with maven first time then you must refer above link.

Create Maven Spring MVC Portlet

Click File–>New–>Liferay Plugin Project it will show plugin project wizard windows as below. You can also use Liferay IDE 3.1.2  GA3

Create new maven Spring MVC Portlet

Click on next and you will see the screen from where you can choose portlet framework.

Maven Spring MVC Portlet - Create Portlet framework

You can see that creating Spring MVC Portlet option is now out of the box from LDS 2.x version or eclipse IDE 3.1.2 GA3. Choose Spring MVC and click Finish. Wizard will create project structure as below.

Maven Spring MVC Portlet - Project Structure

Quick Observation :

  • All your Java code resides in src/main/java folder
  • All your resource files (i.e language properties file) reside in src/main/resources folder
    Wizard created one controller for us.
  • All your static contents (Js, CSS etc) reside in respective folders under src/main/webapp path
  • Wizard had put the view.jsp under src/main/webapp/WEB-INF/jsp/sample-spring-mvc-portlet folder. It’s not required to put the JSPs on this location. You can use any location (even outside of WEB-INF) but then you need to set that path accordingly in spring application context path. (we will see this in short while).
  • Wizard had put 2 spring application context files.

Why two application context files ?

You will see, the wizard put 2 spring application context files in src/main/webapp/WEB-INF/spring-context folder. One is immediate under spring-context while other is inside portlet folder below spring-context folder.

  • portlet-application-context.xml
    • This is directly under spring-context folder.
    • This is global application context file irrespective of any portlet / controller.
    • This will be only useful when you want to implement common functionality across all the portlets defined in your plugin.
    • You can choose custom location but then you need to update its path in web.xml file
  • sample-spring-mvc-portlet-portlet.xml
    • This is under spring-context/portlet folder
    • It’s specific set of controllers defined for particular portlet.
    • Per each portlet, one such application context file is required.
    • You can change the location but then you need to change it’s path in respective portlet entry defined in portlet.xml.

At this point, I will also show you how to put two Maven Spring MVC portlet (s) in single plugin.

For our example, we don’t need the global spring application context file (portlet-application-context.xml) file. Delete it and add following content in sample-spring-mvc-portlet-portlet.xml file

It’s just a path where you can place your JSPs. You many find more information in step 7 of my previous blog Spring MVC portlet in Liferay.

Whe you delete global application context file (portlet-application-context.xml in our case), you need to delete the corresponding entry from web.xml or else it will show error when you deploy the WAR.

Just delete below two sections from web.xml file. It’s located at src–>main–>webapp–>WEB-INF path.

Section 1:

Section 2:

There is no need of this listener as we are deleting global spring application context file.

Keep the following lines of code in web.xml as is. DO NOT Delete them.

This is useful to map spring portlet context to spring web context and it’s required or else it will not work properly.

Renaming portlet

I will be showing how to add multiple Maven Spring MVC Portlet (s) in single liferay plugin WAR. To make it more clear, rename our first portlets and it’s resources as per below steps.

Step 1: Changes in JSP and it’s folder.

  • Rename sample-spring-mvc-portlet folder (under JSP) to portlet1. Update content of view.jsp as below.


Step 2: Rename package and portlet controller name

  • Update the package from com.opensource.techblog.springmvc to com.opensource.techblog.springmvc.portlet1
  • Update the controller name (created by wizard) from PortletViewController to Portlet1ViewController and put following content in it.

Step 3: Changes in portlet specific application context file.

  • Rename sample-spring-mvc-portlet-portlet.xml to portlet1.xml under spring-context/portlet folder
  • You also need to update base package for context:component-scan and corresponding path in viewResolver bean (inside porlet1.xml) so it looks like as below.

Quick observation:

  • Package is changed from com.opensource.techblog.springmvc to com.opensource.techblog.springmvc.portlet1 since we changed the controller package.
  • context:component-scan would ask spring to scan all the classes (on path defined by base-package) having annotation @Controller undercom.opensource.techblog.springmvc.portlet1 package and consider them as controller for request navigation.
  • If you don’t want to write this line, then you have to put <bean> entry for each of your controller manually.

Step 4: Change in portlet.xml file

  • We renamed the spring application context file from sample-spring-mvc-portlet-portlet.xml to portlet1.xml, the same need to be updated in portlet.xml.
  • Open portlet.xml file and update the value of init parameter contextConfigLocation from
    /WEB-INF/spring-context/portlet/sample-spring-mvc-porlet-portlet.xml to /WEB-INF/spring-context/portlet/portlet1.xml
  • Change portlet-name and display-name from sample-spring-mvc-portlet to portlet1 in portlet.xml. You may also change title, short-title and keywords from sample-spring-mvc-portlet to portlet1.

Step 5: Change in liferay-portlet.xml file

  • If you rename the portlet-name, the same need to update in liferay-portlet.xml file. Open liferay-portlet.xml and rename portlet-name from sample-spring-mvc-portlet to portlet1

Step 6: Change in liferay-display.xml file

  • Change portlet Id from sample-spring-mvc-porlet to portlet1(Portlet name that we gave in portlet.xml)

Add new Maven Spring MVC portlet.

You can take help of Liferay developer studio’s wizard to add new Maven spring MVC portlet. But I prefer to add it manually. Let’s do it. Follow the steps below.

Step 1: Add new spring controller java class with different package.

  • You must set different package for different portlets or else it may create issue while finding proper method handler.
  • Create new package called com.opensource.techblog.springmvc.portlet2 and add class Portlet2ViewController. Add following content in it.

Step 2: Add JSP

  • It’s always good idea to have separate folder for each portlet.
  • Create folder called portlet2 under jsp folder and add view.jsp with following content

Step 3: Add new Portlet specific Spring application context file.

  • It’s recommendable to add new portlet specific spring application context file.
  • Add portlet2.xml under spring-context/portlet folder and add following content in it.

Step 4:  Add new portlet entry in portlet.xml, liferay-portlet.xml and liferay-display.xml as below.

  • portlet.xml file

  • liferay-portlet.xml file

  • liferay-display.xml file

At this point the final project structure of our Maven Spring MVC Portlet would be as below.

Maven Spring MVC Portlet - final project structure

POM Structure

open pom.xml which looks as below.

Spring related dependencies are added in dependencies section.

Build and Deploy

It’s time to build and deploy your plugin. Before building, just comment following line under <execution> element in pom.xml

Open command prompt and go to the project path. Execute command mvn clean install liferay:deploy. You can find more info from my previous blog on how to build liferay maven plugin(last section)

You will see BUILD SUCCESS message. Congratulation. Your Maven Spring MVC portlet is completed.

Place it on liferay page and check the output.

Thumb rule

To Add additional spring mvc portlet in single plugin

  • Add new controller. Make sure you choose new java package
  • Add portlet jsp. Make sure you add inside new folder
  • Add portlet specific application context file. In this file, you need to set parent path of JSP to viewResolver bean.
  • Update Liferay specific configuration files
    • portlet.xml
      • Add new portlet entry (with correct portlet name)
      • Make sure you give correct path of portlet specific application context file (that you created in #3 above) in init param
    • liferay-portlet.xml
      • Add new portlet entry (with correct portlet name)
    • liferay-display.xml
      • Add new portlet entry with id as it’s name defined in portlet.xml

It’s always good idea to split your work in multiple spring controller instead just put all your methods in one big controller. In this scenario, you no need to create new Maven Spring MVC Portlet. Just add additional controllers in same package.

And this is done. Express your thoughts in comment section to make this topic more interesting.

Summing Up

  • Spring MVC is one of the most popular framework to create portlet in liferay.
  • Spring MVC portlet will have multiple controllers.
  • mapping of controller and JSP is defined in application context file.
  • Path of application context file is configured in portlet.xml file.
  • To add new portlet, you need to create separate controller package, jsp folder, application context path and have to made separate portlet entry in portlet.xml, liferay-portlet.xml and liferay-display.xml files.



In case if you don’t have Liferay developer studio 2.x, you can download the maven project structure and source from Git Hub Project

Share This Post

3 Comments - Write a Comment

Post Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.