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.
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.
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’sSpringContextLoaderListener
class uses this context file.MySpringMVCPortletviewController
→ MapsVIEW
requests to theview.jsp
and assigns Liferay DXP release information to a model attribute.portlet.xml
→ References context configuration filemy-spring-mvc-portlet.xml
and specifies a dispatcher for registered portlet request handlers.web.xml
→ References context configuration fileportlet-application-context.xml
and specifies aViewRendererServlet
to convert portlet requests and responses to HTTP servlet requests and responses.
Here are the Spring MVC portlet upgrade steps:
-
Adapt the code to Liferay 7.0’s API
-
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 name | New name |
---|---|
spring-web-portlet | spring-webmvc-portlet |
spring-web-servlet | spring-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!
Related Topics
Migrating Plugins SDK Projects to Workspace and Gradle