What It Does & When to Use It
What Does processAction() Do?
processAction() (implemented as doProcessAction() in BaseMVCActionCommand) is executed during the Action Phase of the Portlet Lifecycle.
It is responsible for processing user actions such as form submissions and executing business logic before the Render Phase begins.
Key Responsibilities
- Executes during the ACTION_PHASE
- Triggered by forms using
<portlet:actionURL /> - Handles Create, Update, Delete (CRUD) operations
- Invokes Local Services or External APIs
- Performs validation and business logic
- Sets success/error messages
- Redirects or prepares data for the Render Phase
- Never writes HTML directly to the response
Action Phase Flow
User Clicks Submit │ ▼ <portlet:actionURL> │ ▼ MVCActionCommand (doProcessAction) │ ├── Validate Input ├── Business Logic ├── Call Service Layer ├── Save/Update/Delete Data ├── SessionMessages / SessionErrors └── Optional Redirect │ ▼ Container Automatically Starts Render Phase
Key Rule
An
MVCActionCommandmust never generate HTML or write directly to the response.
Its responsibility is only to process the request and prepare what should happen next.
The valid outputs are:
- Render Parameters
-
SessionMessages -
SessionErrors -
actionResponse.sendRedirect()
Success & Error Messages
Success Message
SessionMessages.add( actionRequest, "employee-added-successfully");
Error Message
SessionErrors.add( actionRequest, "employee-save-failed");
These messages are displayed during the subsequent Render Phase.
Example — Save Employee Action
@Component( property = { "javax.portlet.name=" + EmployeePortletKeys.EMPLOYEE, "mvc.command.name=/save_employee" }, service = MVCActionCommand.class ) public class SaveEmployeeMVCActionCommand extends BaseMVCActionCommand { @Reference private EmployeeLocalService employeeLocalService; @Override protected void doProcessAction( ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { String firstName = ParamUtil.getString( actionRequest, "firstName"); String email = ParamUtil.getString( actionRequest, "email"); employeeLocalService.addEmployee( /* pass required parameters */ ); SessionMessages.add( actionRequest, "employee-added-successfully"); } }
JSP Form
<portlet:actionURL name="/save_employee" var="saveEmployeeURL" /> <aui:form action="<%= saveEmployeeURL %>" method="post"> <aui:input name="firstName" /> <aui:input name="email" /> <aui:button type="submit" /> </aui:form>
Important Note
The value specified in
<portlet:actionURL name="/save_employee" />
must exactly match
mvc.command.name=/save_employee
inside the @Component annotation.
If they don't match, Liferay won't be able to locate and execute your MVCActionCommand.
Best Practices
- Keep business logic inside the Service Layer, not inside the Action Command.
- Use
ParamUtilto safely retrieve request parameters. - Display feedback using
SessionMessagesandSessionErrors. - Catch exceptions and log meaningful error messages.
- Keep the Action Command lightweight—it should coordinate the request, not contain complex business logic.
- Never write HTML or JSON directly from an
MVCActionCommand; let the Render Phase handle the UI.
Summary
MVCActionCommand acts as the Controller of the Action Phase in Liferay MVC. It receives user requests, validates input, invokes business services, stores feedback messages, and hands control back to the container, which automatically starts the Render Phase to display the updated UI
Comments
Post a Comment