MVC Action Command

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:

  1. 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 the var attribute a variable name.

  2. Assign the action URL variable (var) to a UI component. Acting on the UI component invokes the action. For example, the sample’s greetURL action URL variable triggers on submitting this form:

    <aui:form action="<%= greetURL %>" method="post" name="fm">
    	<aui:input name="name" type="text" />
    		<aui:button type="submit"></aui:button>
  3. Create a class that implements the MVCActionCommand interface, or that extends the BaseMVCActionCommand class. The latter may save you time, since it already implements MVCActionCommand.

  4. Annotate your class with an @Component annotation, like this one:

        property = {
        service = MVCActionCommand.class
  5. Set a property to your portlet’s internal ID.

    Note, you can apply MVC Command classes to multiple portlets by setting a property for each portlet. For example, the properties in this component apply it to three specific portlets.

        immediate = true,
        property = {
            "" + BlogsPortletKeys.BLOGS,
            "" + BlogsPortletKeys.BLOGS_ADMIN,
            "" + BlogsPortletKeys.BLOGS_AGGREGATOR,
        service = MVCActionCommand.class
    public class EditEntryMVCActionCommand extends BaseMVCActionCommand {
  6. Set the property to your <portlet:actionURL> tag’s name. This maps your class to the action URL of the same name.

  7. Register your class as an MVCActionCommand service by setting the service attribute to MVCActionCommand.class.

  8. Implement your action logic by overriding the appropriate method of the class you’re implementing or extending.

    Here’s an example of overriding MVCActionCommand’s processAction method. This action logic gets the name parameter from the ActionRequest and adds it to the session messages and to an ActionRequest attribute.

    public boolean processAction(
    		ActionRequest actionRequest, ActionResponse actionResponse)
    	throws PortletException {
    	return true;
    private void _handleActionCommand(ActionRequest actionRequest) {
    	String name = ParamUtil.get(actionRequest, "name", StringPool.BLANK);
    	if (_log.isInfoEnabled()) {"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(

Congratulations! You’ve created an MVCActionCommand that handles your portlet actions.

Creating an MVC Portlet

Configuring the View Layer

MVC Render Command

MVC Resource Command

MVC Command Overrides

« Configuring the View LayerMVC Render Command »
¿Fue útil este artículo?
Usuarios a los que les pareció útil: 0 de 0