Liferay 7:@Reference( target = "(component.name=String.Here)") 如何设置 String.Here?

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

我正在研究 Liferay PortletMVC,我用 @Reference(target = "(component.name=String.Here)", unbind = "-") 注入了 protected MVCActionCommand mvcActionCommand; 来在我的 MVCActionCommand[=21 的 doProcessAction() 方法中执行一些功能=] 组件.

我的问题是如何设置 component.name in the target of @Reference 属性,我应该放置任何字符串还是应该放置已定义的字符串?

component.name 目标 MVC 命令 class 名称。因此,您需要提供 class 名称,包括它的包名称。

要注入实例变量mvcActionCommand,你可以这样使用:

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

我在这里写了一个完整的例子:

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>

检查此 example too on Github

有关覆盖 MVC 命令的更多详细信息,请参阅此 TUTORIAL