在木兰cms中获取已执行的自定义操作的节点名称

时间:2019-11-25 14:53:35

标签: magnolia

我在木兰cms中创建了一个名为MyAction的自定义操作。 我想获取在其上执行操作的页面的节点名称。相反,我得到了一个空字符串的页面名称。

这是代码:

package ch.xxx.module.versioning;

import info.magnolia.ui.api.action.Action;
import info.magnolia.ui.api.action.ActionExecutionException;

import javax.jcr.LoginException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import info.magnolia.context.Context;
import info.magnolia.context.MgnlContext;



public class MyAction implements Action  {


    @Override
    public void execute() throws ActionExecutionException {
        String nodeName= "null";

        Context context = MgnlContext.getInstance();
        Session session = null;
        try {
            session = context.getJCRSession("website");
        } catch (LoginException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RepositoryException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //Get root node
        try {
            nodeName = session.getRootNode().getName();
        } catch (RepositoryException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("Executed MyAction for node: " + nodeName);
    }
}

2 个答案:

答案 0 :(得分:1)

您可以有一个动作的构造函数,并将info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter作为c-tor参数插入。

答案 1 :(得分:0)

感谢@ Ducaz035!

以下是适用于自定义操作的解决方案:

public class MyAction extends AbstractMultiItemAction<zzzVersioning>  {


public MyAction(zzzVersioning definition, JcrItemAdapter item, UiContext uiContext) {
                super(definition, item, uiContext);
                // TODO Auto-generated constructor stub
            }



@Override
public void execute() {
    try {
        System.out.println("Ran execute Action! " + getItems().get(0).getJcrItem().getName());
    } catch (RepositoryException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}



@Override
public void executeOnItem(JcrItemAdapter item) throws Exception {
    // TODO Auto-generated method stub

}



@Override
protected String getSuccessMessage() {
    // TODO Auto-generated method stub
    return null;
}



@Override
protected String getFailureMessage() {
    // TODO Auto-generated method stub
    return null;
}

}

以下是自定义操作定义的代码:

public class zzzVersioning extends CommandActionDefinition {

    public zzzVersioning() {
        this.setImplementationClass(MyAction.class);
    }
}