Generating MVC portlet projects is a snap using Liferay’s project templates. Here you’ll generate an MVC Portlet project and deploy the portlet to Liferay DXP.
-
Generate an MVC Portlet project using a Gradle or Maven.
Here’s the resulting folder structure for an MVC Portlet class named
MyMvcPortlet
in a base packagecom.liferay.docs.mvcportlet
:my-mvc-portlet-project
→ Arbitrary project name.gradle
wrapper
gradle-wrapper.jar
gradle-wrapper.properties
src
main
java
com/liferay/docs/mvcportlet
constants
MyMvcPortletKeys.java
→ Declares portlet constants.
portlet
MyMvcPortlet.java
→ MVC Portlet class.
resources
content
Language.properties
→ Resource bundle
META-INF
resources
init.jsp
→ Imports classes and taglibs and defines commonly used objects from the theme and the portlet.view.jsp
→ Default view template.
bnd.bnd
→ OSGi bundle metadata.build.gradle
gradlew
The Maven-generated project includes a
pom.xml
file and does not include the Gradle-specific files, but otherwise is exactly the same.Here’s the resulting MVC Portlet class:
package com.liferay.docs.mvcportlet.portlet;
import com.liferay.docs.mvcportlet.constants.MyMvcPortletKeys;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import javax.portlet.Portlet;
import org.osgi.service.component.annotations.Component;
@Component(
immediate = true,
property = {
"com.liferay.portlet.display-category=category.sample",
"com.liferay.portlet.instanceable=true",
"javax.portlet.display-name=my-mvc-portlet-project Portlet",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.name=" + MyMvcPortletKeys.MyMvc,
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=power-user,user"
},
service = Portlet.class
)
public class MyMvcPortlet extends MVCPortlet {
}
The class extends
MVCPortlet
.
The
@Component
annotation and service = Portlet.class
attribute makes the class an OSGi
Declarative Services component that provides the
javax.portlet.Portlet
service type. The immediate = true
attribute activates the service immediately
on the portlet’s deployment.
-
Set any portlet configuration or Liferay portlet configuration values using
javax.portlet.*
andcom.liferay.portlet.*
@Component
annotation propertiesjavax.portlet.*
andcom.liferay.portlet.*
@Component
annotation properties respectively.Here are the example component’s properties:
-
"com.liferay.portlet.display-category=category.sample"
: Sets the Widget’s category to “Sample”. -
"com.liferay.portlet.instanceable=true"
: Activates the component immediately when its bundle installs. -
"javax.portlet.display-name=my-mvc-portlet-project Portlet"
: Sets the portlet’s Widget name. -
"javax.portlet.init-param.template-path=/"
: The path undersrc/main/resources/META-INF/resources/
where the templates reside. -
"javax.portlet.init-param.view-template=/view.jsp"
: Default view template. -
"javax.portlet.name=" + MyMvcPortletKeys.MyMvc
: The portlet’s unique identity. -
"javax.portlet.resource-bundle=content.Language"
: Sets the portlet’s resource bundle to thecontent/Language*.properties
file(s) in thesrc/main/resources/
folder. -
"javax.portlet.security-role-ref=power-user,user"
: Makes the Liferay DXP virtual instance’s power user and user Roles available for defining the portlet’s permissions.
-
-
The portlet renders content via the view template
src/main/resources/META-INF/resources/view.jsp
by default. -
Build your project.
Gradle:
gradlew jar
Maven:
mvn clean package
-
Deploy the project using your build environment or by building the project JAR and copying it to the
deploy/
folder in your Liferay Home.
The MVC Portlet is now available in the Liferay DXP UI, in the Widget category you assigned it.
Congratulations on creating and deploying an MVC Portlet!