Creating an MVC Portlet

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.

  1. 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 package

    • my-mvc-portlet-project → Arbitrary project name.
      • gradle
        • wrapper
          • gradle-wrapper.jar
      • src
        • main
          • java
            • com/liferay/docs/mvcportlet
              • constants
                • → Declares portlet constants.
              • portlet
                • → MVC Portlet class.
          • resources
            • content
              • → 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:


import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import javax.portlet.Portlet;
import org.osgi.service.component.annotations.Component;

	immediate = true,
	property = {
		"javax.portlet.display-name=my-mvc-portlet-project Portlet",
		"" + MyMvcPortletKeys.MyMvc,
	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.

  1. Set any portlet configuration or Liferay portlet configuration values using javax.portlet.* and com.liferay.portlet.* @Component annotation properties javax.portlet.* and com.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 under src/main/resources/META-INF/resources/ where the templates reside.

    • "javax.portlet.init-param.view-template=/view.jsp": Default view template.

    • "" + MyMvcPortletKeys.MyMvc: The portlet’s unique identity.

    • "javax.portlet.resource-bundle=content.Language": Sets the portlet’s resource bundle to the content/Language*.properties file(s) in the src/main/resources/ folder.

    • ",user": Makes the Liferay DXP virtual instance’s power user and user Roles available for defining the portlet’s permissions.

  2. The portlet renders content via the view template src/main/resources/META-INF/resources/view.jsp by default.

  3. Build your project.


    gradlew jar


    mvn clean package
  4. 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.

Figure 1: The example portlet shows a message defined by the language property yourmvc.caption=Hello from YourMVC! in the file.

Figure 1: The example portlet shows a message defined by the language property `yourmvc.caption=Hello from YourMVC!` in the file.

Congratulations on creating and deploying an MVC Portlet!

Writing MVC Portlet Controller Code

Configuring the View Layer

MVC Action Command

MVC Render Command

MVC Resource Command

« Liferay MVC PortletWriting MVC Portlet Controller Code »
1 人中有 0 人觉得有帮助