Overriding MVCResourceCommands

If you need to add functionality to a Liferay MVC resource command, you can. The Liferay MVC command framework supports customizing MVC resource commands. It follows the process for adding logic to MVC commands and it is similar to the ones described for MVCRenderCommand and MVCActionCommand. There’s a couple things to keep in mind:

  • The service to specify in your component is MVCResourceCommand.class

  • As with overriding MVCRenderCommand, there’s no base implementation class to extend. Implement the MVCResourceCommand interface yourself.

  • Keep your code decoupled from the original code by adding your logic to the original MVCResourceCommand’s logic by getting a reference to the original and returning a call to its serveResource method:

return mvcResourceCommand.serveResource(resourceRequest, resourceResponse);

The following example overrides the behavior of com.liferay.login.web.portlet.action.CaptchaMVCResourceCommand, from the Liferay’s Login portlet’s login-web module. It simply prints a line in the console and then executes the original logic: returning the Captcha image for the account creation screen.

    property = { 
        "javax.portlet.name=" + LoginPortletKeys.LOGIN,
    service = MVCResourceCommand.class
public class CustomCaptchaMVCResourceCommand implements MVCResourceCommand {

    public boolean serveResource
        (ResourceRequest resourceRequest, ResourceResponse resourceResponse) {

        System.out.println("Serving login captcha image");

        return mvcResourceCommand.serveResource(resourceRequest, resourceResponse);

    @Reference(target = 
    protected MVCResourceCommand mvcResourceCommand;

And that, as they say, is that. Even if you don’t own the source code of an application, you can override its MVC commands just by knowing the component class name.

« Overriding MVCActionCommandsIntroduction to Overriding OSGi Services »
Was this article helpful?
0 out of 0 found this helpful