获取在 magnolia cms 中执行的自定义操作的节点名称
Get node name for the executed custom action in magnolia cms
我在 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);
}
}
您可以拥有一个构造函数并将 info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter
作为 c-tor 参数注入。
谢谢@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);
}
}
我在 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);
}
}
您可以拥有一个构造函数并将 info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter
作为 c-tor 参数注入。
谢谢@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);
}
}