Liferay’s MVC Portlet framework enables you to handle MVCPortlet actions in separate classes. This facilitates managing action logic in portlets that have many actions. Each action URL in your portlet’s JSPs invokes an appropriate action command class.
Here are the steps:
-
Configure your JSPs to use action URLs via
<portlet:actionURL>
tags. For example, the action-command-portlet sample uses this action URL:<liferay-portlet:actionURL name="greet" var="greetURL" />
Name the action URL via its
name
attribute. Your*MVCActionCommand
class maps to this name. Assign thevar
attribute a variable name. -
Assign the action URL variable (
var
) to a UI component. Acting on the UI component invokes the action. For example, the sample’sgreetURL
action URL variable triggers on submitting this form:<aui:form action="<%= greetURL %>" method="post" name="fm"> <aui:input name="name" type="text" /> <aui:button-row> <aui:button type="submit"></aui:button> </aui:button-row> </aui:form>
-
Create a class that implements the
MVCActionCommand
interface, or that extends theBaseMVCActionCommand
class. The latter may save you time, since it already implementsMVCActionCommand
. -
Annotate your class with an
@Component
annotation, like this one:@Component( property = { "javax.portlet.name=your_portlet_name_YourPortlet", "mvc.command.name=/your/jsp/action/url" }, service = MVCActionCommand.class )
-
Set a
javax.portlet.name
property to your portlet’s internal ID.Note, you can apply MVC Command classes to multiple portlets by setting a
javax.portlet.name
property for each portlet. For example, thejavax.portlet.name
properties in this component apply it to three specific portlets.@Component( immediate = true, property = { "javax.portlet.name=" + BlogsPortletKeys.BLOGS, "javax.portlet.name=" + BlogsPortletKeys.BLOGS_ADMIN, "javax.portlet.name=" + BlogsPortletKeys.BLOGS_AGGREGATOR, "mvc.command.name=/blogs/edit_entry" }, service = MVCActionCommand.class ) public class EditEntryMVCActionCommand extends BaseMVCActionCommand { ... }
-
Set the
mvc.command.name
property to your<portlet:actionURL>
tag’sname
. This maps your class to the action URL of the same name. -
Register your class as an
MVCActionCommand
service by setting theservice
attribute toMVCActionCommand.class
. -
Implement your action logic by overriding the appropriate method of the class you’re implementing or extending.
-
MVCActionCommand
implementations override theprocessAction
method. -
BaseMVCActionCommand
extensions override thedoProcessAction
method.
Here’s an example of overriding
MVCActionCommand
’sprocessAction
method. This action logic gets the name parameter from theActionRequest
and adds it to the session messages and to anActionRequest
attribute.@Override public boolean processAction( ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException { _handleActionCommand(actionRequest); return true; } private void _handleActionCommand(ActionRequest actionRequest) { String name = ParamUtil.get(actionRequest, "name", StringPool.BLANK); if (_log.isInfoEnabled()) { _log.info("Hello " + name); } String greetingMessage = "Hello " + name + "! Welcome to OSGi"; actionRequest.setAttribute("GREETER_MESSAGE", greetingMessage); SessionMessages.add(actionRequest, "greetingMessage", greetingMessage); } private static final Log _log = LogFactoryUtil.getLog( GreeterActionCommand.class);
-
Congratulations! You’ve created an MVCActionCommand
that handles your portlet
actions.