Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Override Portlet MVC Action Command in Liferay 7

    • 0
    • 1
    • 0
    • 0
    • 7
    • 0
    • 0
    • 0
    • 3.67k
    Comment on it

    Hello Programmers,


    If you are looking to override a Portlet MVC Action Command then you are at correct place. I'm going to override LoginMVCActionCommand in this example. I'm writing the steps here. Before starting code, you should read the following blogs.




    Hope you gone through the blogs. In first blog, If you see the complete output for the command g! b 217 then you will find the following in the result.


    {com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand}={,[com_liferay_login_web_portlet_FastLoginPortlet,com_liferay_login_web_portlet_LoginPortlet],,,, service.bundleid=217, service.scope=bundle}


    Notice the followings properties and values. These are very important to override a portlet MVC action command. 



    Let's understand the above values. 

    • : a key to identify the particular command. It is the value of name property in your JSP's matching actionUrl
    • : the portlet that is affected by this command.
    • : component weight.


    Let's override the login action command.

    1. Create Liferay Module Project in a Liferay Workspace Project. I have created a module login-action-hook under workspace project evon-module
    2. Right click on module project -> New -> Others.
    3. Search Liferay Component Class and select it. Check Image 1 below.
    4. Select your Project name from drop down. Provide Package name, Component Class Name and select Portlet Action Command from drop down for Component Class Template. Check Image 2 below.
    5. Click Finish.


                                                                      Image 1



                                                                      Image 2



    IDE creates the classes with some default annotation and code in your action command class. Open file and change the code. You can delete the unnecessary classes other than your ActionCommand class.


    Please notice - We need to resolve the dependencies manually in case of different components. In case of Portlet Action Command, here are the build.gradle dependencies.


    dependencies {
    	compileOnly group: "com.liferay", name: "com.liferay.frontend.taglib", version: "2.0.0"
    	compileOnly group: "com.liferay", name: "com.liferay.petra.content", version: "1.0.0"
    	compileOnly group: "com.liferay", name: "com.liferay.portal.upgrade", version: "2.0.0"
    	compileOnly group: "com.liferay.portal", name: "com.liferay.portal.impl", version: "2.0.0"
    	compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.7.0"
    	compileOnly group: "com.liferay.portal", name: "", version: "2.0.0"
    	compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "2.0.0"
    	compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"
    	compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1"
    	compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.3.0"


    Once changed the dependencies, right click on module -> Gradle -> Refresh the project. It will take time to resolve and download the dependencies.


    We already found out the and properties for the LoginMCVActionCommand. Let's keep the values in our component annotation.


    package com.evontech.action;
    import javax.portlet.ActionRequest;
    import javax.portlet.ActionResponse;
    import javax.portlet.PortletException;
    import org.osgi.service.component.annotations.Component;
    import org.osgi.service.component.annotations.Reference;
    import com.liferay.portal.kernel.log.Log;
    import com.liferay.portal.kernel.log.LogFactoryUtil;
    import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCActionCommand;
    import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand;
    		immediate = true,
    		property = {
    		service = MVCActionCommand.class
    public class EvonLoginMVCActionCommand extends BaseMVCActionCommand {
    	private static final Log _log = LogFactoryUtil.getLog(EvonLoginMVCActionCommand.class);
    	@Reference(target = "(&(")
    	protected MVCActionCommand mvcActionCommand;	
    	public EvonLoginMVCActionCommand() {
   "**** EvonLoginMVCActionCommand Constructor ****");
    	protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
   "*************** My Login ************* ");	
    		mvcActionCommand.processAction(actionRequest, actionResponse);


    I changed the default annotation and the default code. Let's understand it.


    1. Check the component annotation. I've kept the values that we found from gogo shell. The properties let know the OSGI runtime that which action command is overriden. I've used the service.ranking:Integer=161 here. This property overrides the and used to tell the OSGi runtime which service to use, in cases where there are multiple components registering the same service, with the same properties. The higher the integer you specify here, the more weight your component carries. Therefore I used 161 to override my default value 160.
    2. I've used @Reference annotation to target the original LoginMVCActionCommand object so that I can use the default functionality along with my code. Please notice, I've used the ( inside the annotation to avoide the circular reference detection issue.
    3.  You can write your custom code in doProcessAction method.

    Deploy the code and you will see your logs in log window.


    That's all.

    Hope that would help you.

 7 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: