【问题标题】:Get node name for the executed custom action in magnolia cms获取 magnolia 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);
    }
}

【问题讨论】:

  • 您的 Magnolia 版本是多少?
  • Magnolia 版本为 5.6.11
  • 你做了什么:nodeName = session.getRootNode().getName(); 正在获取根节点的名称 (/),该名称始终为空。

标签: magnolia


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    感谢@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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      • 2021-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多