Capabilities
Product
Education
Knowledge Base
Contact Us
-
It’s an extension of GenericPortlet. - You avoid writing a bunch of boilerplate code, since Liferay’s MVC Portlet framework only looks for some pre-defined parameters when the
init()method is called. - The controller can be broken down into MVC command classes, each of which handles the controller code for a particular portlet phase (render, action, and resource serving phases).
- MVC command classes can serve multiple portlets.
- Liferay’s portlets use it. That means there are plenty of robust implementations to reference when you need to design or troubleshoot your Liferay applications.
The Liferay MVC Portlet framework is light and easy to use.
Here, you’ll learn how MVC Portlet works by covering these topics:
- MVC layers and modularity
- Liferay MVC command classes
- Liferay MVC portlet component
- Simple MVC portlets
Consider how each layer of the Liferay MVC portlet framework helps you separate the concerns of your application.
MVC Layers and Modularity
In MVC, there are three layers.
Model: The model layer holds the application data and logic for manipulating it.
View: The view layer displays data.
Controller: The middle man in the MVC pattern, the Controller passes the data back and forth between the view and the model layers.
Liferay DXP’s applications are divided into multiple discrete modules. With Service Builder, the model layer is generated into service and api modules. The view and the controller layers share a module, the web module.
Generating the skeleton for a multi-module Service Builder-driven MVC application project using Workspace saves you lots of time and gets you started on the more important (and interesting, if we’re being honest) development work.
Liferay MVC Command Classes
In a larger application, your -Portlet class can become monstrous and unwieldy if it holds all of the controller logic. Liferay provides MVC command classes to break up your controller functionality.
MVCActionCommand: Use-ActionCommandclasses to hold each of your portlet actions, which are invoked by action URLs.MVCRenderCommand: Use-RenderCommandclasses to hold arendermethod that dispatches to the appropriate JSP, by responding to render URLs.MVCResourceCommand: Use-ResourceCommandclasses to serve resources based on resource URLs.
There must be some confusing configuration files to keep everything wired together and working properly, right? Wrong: it’s all easily managed in the -Portlet class’s @Component annotation.
Liferay MVC Portlet Component
Whether or not you plan to split up the controller into MVC command classes, the portlet @Component annotation configures the portlet. Here’s a simple portlet component as an example:
@Component(
property = {
"com.liferay.portlet.display-category=category.sample",
"javax.portlet.display-name=Hello World Portlet",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.name=com_acme_hello_world_web_internal_portlet_HelloWorldPortlet"
},
service = Portlet.class
)
public class HelloWorldPortlet extends MVCPortlet {
The javax.portlet.name property is required. When using MVC commands, the javax.portlet.name property value links particular portlet URL/command combinations to the correct portlet.
Make your portlet name unique, considering how [Liferay DXP uses the name to create the portlet’s ID.
There can be some confusion over exactly what kind of Portlet.class implementation you’re publishing with a component. The service registry expects this to be the javax.portlet.Portlet interface. Import that, and not, for example, com.liferay.portal.kernel.model.Portlet.