Search code examples
liferayosgiportletliferay-7

Liferay7: @Reference( target = "(component.name=String.Here)") How can I set String.Here?


I'm working on Liferay PortletMVC, I injected the protected MVCActionCommand mvcActionCommand; with the @Reference(target = "(component.name=String.Here)", unbind = "-") to do some functions stuff inside the doProcessAction() method of my MVCActionCommand component.

My question is how can I set the component.name in the target of @Reference attribute, Should I put any String or should I put a defined one ?


Solution

  • component.name target the MVC command class name. So you need to provide the class name including it's package name.

    To inject the instance variable mvcActionCommand, you can use something like this:

      @Reference(target = "(component.name=com.test.service.impl.TestServiceImp)", 
                unbind = "-")
        public void setMvcActionCommand(MVCActionCommand mvcActionCommand) {
            this.mvcActionCommand = mvcActionCommand;
        }
    

    I wrote a full example here:

    JAVA:

    @Component(
            immediate = true,
            property = {
                    "javax.portlet.name=YOU_COMPONENT_NAME",
                    "mvc.command.name=/hello"
            },
            service = MVCActionCommand.class
    )
    public class LoginTestCommand extends BaseMVCActionCommand {
    
        protected MVCActionCommand mvcActionCommand;
    
        @Reference(target = "(component.name=com.liferay.login.web.internal.portlet.action.LoginMVCActionCommand)",
                unbind = "-")
        public void setMvcActionCommand(MVCActionCommand mvcActionCommand) {
            this.mvcActionCommand = mvcActionCommand;
        }
    
        @Override
        protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
            System.out.println( "Login account doProcessAction" );
            mvcActionCommand.processAction(actionRequest, actionResponse);
        }
    }
    

    JSP:

    <portlet:actionURL var="sayHelloURL" name="/hello">
        <portlet:param name="mvcActionCommand" value="/hello" />
    </portlet:actionURL>
    

    Check this example too on Github.

    See this TUTORIAL for more details about Overriding MVC Commands.