Struts is a stable, widely adopted framework that implements the Model View Controller (MVC) design pattern. If you have a Struts portlet for Liferay Portal 6.2, you can upgrade it to Liferay DXP 7.0.
Upgrading Struts portlets to Liferay DXP 7.0 is easier than you might think. Liferay DXP lets you continue working with Struts portlets as Java EE web applications. On deploying a Struts portlet Web Application aRchive (WAR), Liferay DXP’s Web Application Bundle (WAB) Generator creates an OSGi module (bundle) for the portlet and installs it to Liferay’s OSGi framework. The Struts portlet behaves just as it did in 6.2 on your Liferay DXP 7.0 site.
This tutorial demonstrates how to upgrade a portlet that uses the Struts 1
Framework and refers to Liferay’s Sample Struts portlet (Sample Struts) as
an example. Sample Struts uses several Struts features to show page
navigation, Action
and ActionForm
Controller classes, exceptions, and
more.
Figure 1: The Sample Struts portlet's charts compare fictitious soft drink survey results.
Here are the sample portlet’s characteristics:
- Model class (
Book
) - View comprised of JSPs that leverage Struts tag libraries
Action
classes (Controllers) for handling requests and responsesActionForm
classes for interacting with models and forwarding requests toAction
s- Visual component reuse with Tiles
- Internationalization using Action Messages and resource bundles
- Form validation using the Validator Framework
- Error management through Action Errors
You can follow this tutorial to upgrade your Struts portlet. Along the way, you can examine the Sample Struts portlet source code from before and after its upgrade:
Here’s the Sample Struts portlet’s folder structure:
sample-struts-portlet
docroot/
html/portlet/sample_struts_portlet/
→ JSPsWEB-INF/
lib/
→ Required third-party libraries unavailable in the Liferay DXP systemsrc/
com/liferay/samplestruts/model/
→ Model classescom/liferay/samplestruts/servlet/
→ Test servlet and servlet context listenercom/liferay/samplestruts/struts/
action/
→Action
classes that return View pages to the clientform/
→ActionForm
classes for model interactionrender/
→Action
classes that present additional pages and handle inputSampleException.java
→ Exception class
content/test/
→ Resource bundlesMETA-INF/
→ Javadoc
tld/
→ Tag library definitionsliferay-display.xml
→ Sets the application categoryliferay-plugin-package.properties
→ Sets metadata and portal dependenciesliferay-portlet.xml
→ Maps descriptive role names to rolesliferay-releng.properties
→ (internal) Release propertiesportlet.xml
→ Defines the portlet and its initialization parameters and security rolesstruts-config.xml
→ Struts configurationtiles-defs.xml
→ Struts Tile definitionsvalidation.xml
→ Defines form inputs for validationvalidation-rules.xml
→ Struts validation rulesweb.xml
→ Web application descriptor
build.xml
→ Apache Ant build file
Upgrading a Struts portlet involves these steps:
-
Adapt the code to Liferay 7.0’s API
-
Resolve dependencies
Adapting the code to Liferay 7.0’s API
Liferay’s Upgrade Planner identifies code affected by the new API, explains the API changes and how to adapt to them, and in many cases, provides options for adapting the code automatically.
Adapting the Sample Struts portlet’s code is straightforward. Resolving its dependencies is more involved.
Resolving dependencies
The Liferay Portal 6.2 Sample Struts portlet depends on Liferay Portal to
provide required third-party libraries and tag library definitions (TLDs). The
portal-dependency-jars
and portal-dependency-tlds
properties in the
portlet’s liferay-plugin-package.properties
specifies them:
portal-dependency-jars=\
antlr2.jar,\
commons-beanutils.jar,\
commons-collections.jar,\
commons-digester.jar,\
commons-fileupload.jar,\
commons-io.jar,\
commons-lang.jar,\
commons-validator.jar,\
jcommon.jar,\
jfreechart.jar,\
oro.jar,\
portals-bridges.jar,\
struts.jar
portal-dependency-tlds=\
struts-bean.tld,\
struts-bean-el.tld,\
struts-html.tld,\
struts-html-el.tld,\
struts-logic.tld,\
struts-logic-el.tld,\
struts-nested.tld,\
struts-tiles.tld,\
struts-tiles-el.tld
Resolving the tag libraries is easy.
Resolving Tag Library Definitions
Liferay DXP 7.0 continues to provide many of the same TLDs Liferay Portal 6.2 provided.
If the Liferay DXP 7.0 application’s WEB-INF/tld
folder contains a TLD you need,
add it to your portlet’s portal-dependency-tlds
property in the
liferay-plugin-package.properties
file. If the folder doesn’t contain the TLD,
find the TLD on the web, download it, and add it to your portlet’s WEB-INF/tld
folder.
Resolving Third-Party Libraries
Third-party libraries listed as portal-dependency-jars
in a 6.x portlet’s
liferay-plugin-package.properties
file might not be provided by Liferay DXP 7.0.
Providing fewer libraries streamlines Liferay DXP. Liferay DXP has replaced some of
its libraries with newer ones.
Liferay DXP 7.0 exposes (exports) Java packages instead of sharing JAR content
wholesale. If you need packages Liferay DXP doesn’t export, you can find and
download the artifact (JAR) that provides them and add it to your portlet’s
docroot/WEB-INF/lib
folder.
Here are steps for resolving the Sample Struts portlet’s Java package dependencies:
-
Liferay DXP doesn’t export Antlr packages. Replace the portlet’s JAR file
antlr2.jar
with newer JARantlr.jar
from Maven Central. -
Liferay DXP doesn’t export packages from
portals-bridges.jar
. Replace the portlet’s JAR fileportals-bridges.jar
with these JARs from Maven Central:portals-bridges-common.jar
portals-bridges-struts.jar
-
Liferay DXP doesn’t export packages from
struts.jar
. Replace the portlet’s JAR filestruts.jar
with the following ones from Maven Central:struts-taglib.jar
struts-tiles.jar
-
The
Import-Packages
heading in thecom.liferay.portal.bootstrap
module’ssystem.packages.extra.bnd
file lists the following JAR files Sample Struts requires. Since the bootstrap module exports packages from these JARs, add their names to theportal-dependency-jars
property in the portlet’sliferay-plugin-package.properties
file. :commons-beanutils.jar
commons-collections.jar
commons-lang.jar
-
Add all the rest of the JARs the Sample Struts portlet depends on to the portlet’s
WEB-INF/lib
folder.
The following table summarizes the sample portlet’s Java dependency resolution.
Sample Struts Portlet’s Dependency Resolution:
JAR | System exports the packages the portletneeds from this JAR? | Resolution |
---|---|---|
antlr.jar | No | add to WEB-INF/lib |
commons-beanutils.jar | Yes | List in portal-dependency-jars |
commons-collections.jar | Yes | List in portal-dependency-jars |
commons-digester.jar | No | add to WEB-INF/lib |
commons-fileupload.jar | No | add to WEB-INF/lib |
commons-io.jar | No | add to WEB-INF/lib |
commons-lang.jar | Yes | List in portal-dependency-jars |
commons-validator.jar | No | Add to WEB-INF/lib |
jcommon.jar | No | Add to WEB-INF/lib |
jfreechart.jar | No | Add to WEB-INF/lib |
oro.jar | No | Add to WEB-INF/lib |
portals-bridges-common.jar | No | Add to WEB-INF/lib |
portals-bridges-struts.jar | No | Add to WEB-INF/lib |
struts-core.jar | No | Add to WEB-INF/lib |
struts-extras.jar | No | Add to WEB-INF/lib |
struts-taglib.jar | No | Add to WEB-INF/lib |
struts-tiles.jar | No | Add to WEB-INF/lib |
For more details on resolving dependencies, see the tutorial Resolving a Plugin’s Dependencies.
You’ve resolved the Sample Struts portlet’s dependencies. It’s ready to deploy.
Deploy the Struts portlet as you normally would. The server prints messages indicating the following portlet status:
- WAR processing
- WAB startup
- Availability to users
Deploying the Sample Struts portlet produces these messages:
00:15:20,344 INFO [com.liferay.portal.kernel.deploy.auto.AutoDeployScanner][AutoDeployDir:252] Processing sample-struts-portlet-7.0.0.1.war
00:15:26,871 INFO [fileinstall-C:/portals/liferay-dxp-digital-enterprise-7.0-sp1/osgi/war][BaseAutoDeployListener:42] Copying portlets for C:\portals\liferay-dxp-digital-enterprise-7.0-sp1\tomcat-8.0.32\temp\20170727241526847GURDCOLU\sample-struts-portlet-7.0.0.1.war
00:15:27,282 INFO [fileinstall-C:/portals/liferay-dxp-digital-enterprise-7.0-sp1/osgi/war][BaseDeployer:863] Deploying sample-struts-portlet-7.0.0.1.war
00:15:29,627 INFO [fileinstall-C:/portals/liferay-dxp-digital-enterprise-7.0-sp1/osgi/war][BaseAutoDeployListener:50] Portlets for C:\portals\liferay-dxp-digital-enterprise-7.0-sp1\tomcat-8.0.32\temp\20170727241526847GURDCOLU\sample-struts-portlet-7.0.0.1.war copied successfully
00:15:29,644 WARN [fileinstall-C:/portals/liferay-dxp-digital-enterprise-7.0-sp1/osgi/war][WabProcessor:564] The property "portal-dependency-jars" is deprecated. Specified JARs may not be included in the class path.
00:15:33,123 INFO [fileinstall-C:/portals/liferay-dxp-digital-enterprise-7.0-sp1/osgi/war][BundleStartStopLogger:35] STARTED sample-struts-portlet_7.0.0.1 [1230]
00:15:34,063 INFO [fileinstall-C:/portals/liferay-dxp-digital-enterprise-7.0-sp1/osgi/war][HotDeployImpl:226] Deploying sample-struts-portlet from queue
00:15:34,065 INFO [fileinstall-C:/portals/liferay-dxp-digital-enterprise-7.0-sp1/osgi/war][PluginPackageUtil:1007] Reading plugin package for sample-struts-portlet
00:15:34,106 INFO [fileinstall-C:/portals/liferay-dxp-digital-enterprise-7.0-sp1/osgi/war][PortletHotDeployListener:201] Registering portlets for sample-struts-portlet
00:15:34,424 INFO [fileinstall-C:/portals/liferay-dxp-digital-enterprise-7.0-sp1/osgi/war][PortletHotDeployListener:313] 1 portlet for sample-struts-portlet is available for use
The Struts portlet is available on your Liferay DXP instance.
Congratulations on upgrading your Struts portlet to Liferay DXP 7.0!