Upgrading a Spring MVC portlet

The Spring Portlet MVC framework facilitates injecting dependencies and implementing the Model View Controller pattern in portlets. If you use this framework in a portlet for Liferay Portal 6.x, you can upgrade it to Liferay DXP 7.0.

This tutorial demonstrates upgrading a Spring MVC portlet called My Spring MVC (project my-spring-mvc-portlet). It’s a bare-bones portlet created from the Plugins SDK’s spring_mvc template.

Figure 1: My Spring MVC portlet shows its name and Liferay DXPs information.

Figure 1: My Spring MVC portlet shows its name and Liferay DXP's information.

To follow along, download and refer to the original source code and the upgraded source code.

The figure below shows the my-spring-mvc-portlet project.

Figure 2: The my-spring-mvc-portlet project has traditional Liferay plugin files, Spring Portlet MVC application contexts (in spring-context/), and a controller class MySpringMVCPortletviewController.

Figure 2: The `my-spring-mvc-portlet` project has traditional Liferay plugin files, Spring Portlet MVC application contexts (in `spring-context/`), and a controller class `MySpringMVCPortletviewController`.

These files have Spring-related content:

  • view.jsp → Shows the portlet’s name and Liferay DXP’s release information.
  • my-spring-mvc-portlet.xml → Liferay DXP uses this context file for the portlet.
  • portlet-applications-context.xml → Spring’s SpringContextLoaderListener class uses this context file.
  • MySpringMVCPortletviewController → Maps VIEW requests to the view.jsp and assigns Liferay DXP release information to a model attribute.
  • portlet.xml → References context configuration file my-spring-mvc-portlet.xml and specifies a dispatcher for registered portlet request handlers.
  • web.xml → References context configuration file portlet-application-context.xml and specifies a ViewRendererServlet to convert portlet requests and responses to HTTP servlet requests and responses.

Here are the Spring MVC portlet upgrade steps:

  1. Adapt the code to Liferay 7.0’s API

  2. Resolve dependencies

Adapt the code to Liferay 7.0’s API

The Upgrade Planner facilitates updating the code and resolving compilation issues quickly.

The Upgrade Planner detects if the value of the liferay-versions property in your plugin’s liferay-plugin-package.properties file needs updating and it provides an option to fix it automatically. This is the only code adaptation required by my-spring-mvc-portlet.

Resolve Dependencies

In Liferay Portal 6.2, my-spring-mvc-portlet leveraged Portal’s JARs by specifying them in the liferay-plugin-package.properties file’s portal-dependency-jars property. Since the property is deprecated in Liferay DXP 7.0, you should acquire dependencies using a dependency management framework, such as Gradle, Maven, or Apache Ant/Ivy.

Converting the sample portlet plugin from a traditional plugin to a Liferay Workspace web application facilitated resolving its dependencies.

Here’s the updated my-spring-mvc-portlet’s build.gradle file:

dependencies {
	compileOnly group: 'aopalliance', name: 'aopalliance', version: '1.0'
	compileOnly group: 'commons-logging', name: 'commons-logging', version: '1.2'
	compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
	compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"
	compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1"
	compile group: 'org.jboss.arquillian.junit', name: 'arquillian-junit-container', version: '1.1.3.Final'
	compile group: 'org.jboss.arquillian.container', name: 'arquillian-tomcat-remote-7', version: '1.0.0.CR6'
	compile group: 'com.liferay', name: 'com.liferay.ant.arquillian', version: '1.0.0-SNAPSHOT'
	compile group: 'org.springframework', name: 'spring-aop', version: '4.1.9.RELEASE'
	compile group: 'org.springframework', name: 'spring-beans', version: '4.1.9.RELEASE'
	compile group: 'org.springframework', name: 'spring-context', version: '4.1.9.RELEASE'
	compile group: 'org.springframework', name: 'spring-core', version: '4.1.9.RELEASE'
	compile group: 'org.springframework', name: 'spring-expression', version: '4.1.9.RELEASE'
	compile group: 'org.springframework', name: 'spring-web', version: '4.1.9.RELEASE'
	compile group: 'org.springframework', name: 'spring-webmvc', version: '4.1.9.RELEASE'
	compile group: 'org.springframework', name: 'spring-webmvc-portlet', version: '4.1.9.RELEASE'
}

Some of my-spring-mvc-portlet’s dependency artifacts have new names.

Old nameNew name
spring-web-portletspring-webmvc-portlet
spring-web-servletspring-webmvc

Maven Central provides artifact dependency information.

To import class packages referenced by your portlet’s descriptor files, add the packages to an Import-Package header in the liferay-plugin-package.properties file. See Packaging a Spring MVC Portlet for details.

If you depend on a package from Java’s rt.jar other than its java.* packages, override portal property org.osgi.framework.bootdelegation and add it to the property’s list. Go here for details.

The portlet is ready to deploy. Deploy it as you always have.

Liferay DXP’s WAB Generator converts the portlet WAR to a Web Application Bundle (WAB) and installs the WAB to Liferay’s OSGi Runtime Framework.

21:12:23,775 INFO  [com.liferay.portal.kernel.deploy.auto.AutoDeployScanner][AutoDeployDir:252] Processing my-spring-mvc-portlet-7.0.0.1.war
...
21:12:36,159 INFO  [fileinstall-C:/portals/liferay-dxp-digital-enterprise-7.0-sp1/osgi/war][PluginPackageUtil:1007] Reading plugin package for my-spring-mvc-portlet
07-Aug-2017 21:12:36.170 INFO [fileinstall-C:/portals/liferay-dxp-digital-enterprise-7.0-sp1/osgi/war] org.apache.catalina.core.ApplicationContext.log Initializing Spring root WebApplicationContext
07-Aug-2017 21:12:36.181 INFO [fileinstall-C:/portals/liferay-dxp-digital-enterprise-7.0-sp1/osgi/war] org.apache.catalina.core.ApplicationContext.log Initializing Spring root WebApplicationContext
21:12:36,365 INFO  [fileinstall-C:/portals/liferay-dxp-digital-enterprise-7.0-sp1/osgi/war][PortletHotDeployListener:201] Registering portlets for my-spring-mvc-portlet
21:12:36,707 INFO  [fileinstall-C:/portals/liferay-dxp-digital-enterprise-7.0-sp1/osgi/war][PortletHotDeployListener:313] 1 portlet for my-spring-mvc-portlet is available for use
21:12:36,868 INFO  [fileinstall-C:/portals/liferay-dxp-digital-enterprise-7.0-sp1/osgi/war][BundleStartStopLogger:35] STARTED my-spring-mvc-portlet_7.0.0.1 [1309]

You’ve upgraded a Spring MVC portlet to Liferay DXP 7.0. Way to go!

Spring MVC

Migrating Plugins SDK Projects to Workspace and Gradle

Using Dependency Management Tools

Using the WAB Generator

« Upgrading a Struts PortletUpgrading Web Plugins »
Was this article helpful?
0 out of 0 found this helpful