Overriding MVCActionCommands

In case you want add to a Liferay MVC action command, you can. The OSGi framework lets you override MVC action commands if you follow the instructions for adding logic to MVC commands. It involves registering your custom MVC action command as an OSGi component with the same properties as the original, but with a higher service ranking.

Custom MVC action commands typically extend the BaseMVCActionCommand class, and override its doProcessAction method, which returns void. Add your logic to the original behavior of the action method by getting a reference to the original service, and calling it after your own logic. For example, this MVCActionCommand override checks whether the delete action is invoked on a blog entry, and prints a message to the log, before continuing with the original processing:

@Component(
    property = { 
        "javax.portlet.name=" + BlogsPortletKeys.BLOGS_ADMIN, 
        "mvc.command.name=/blogs/edit_entry",
        "service.ranking:Integer=100" 
    }, 
    service = MVCActionCommand.class
)
public class CustomBlogsMVCActionCommand extends BaseMVCActionCommand {

    @Override
    protected void doProcessAction
        (ActionRequest actionRequest, ActionResponse actionResponse) 
        throws Exception {

        String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

        if (cmd.equals(Constants.DELETE)) {
            System.out.println("Deleting a Blog Entry");
        }

        mvcActionCommand.processAction(actionRequest, actionResponse);
    }

    @Reference(
        target = "(component.name=com.liferay.blogs.web.internal.portlet.action.EditEntryMVCActionCommand)")
    protected MVCActionCommand mvcActionCommand;

}

Adding MVC action command logic before existing logic is straightforward and maintains loose coupling between new and old code.

MVC Action Command

Adding Logic to MVC Commands

Overriding MVCRenderCommands

Converting StrutsActionWrappers to MVCCommands

« Overriding MVCRenderCommandsOverriding MVCResourceCommands »
この記事は役に立ちましたか?
0人中0人がこの記事が役に立ったと言っています