[英]AEM get parent node from Valuemapvalue filepath in sling model
I need to get the parent folder name (String type), once a user provides a filepath in cq dialog.一旦用户在 cq 对话框中提供文件路径,我需要获取父文件夹名称(字符串类型)。 This is my approach:这是我的方法:
import lombok.Getter;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.*;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
@Getter
@Model(adaptables = {
Resource.class,
SlingHttpServletRequest.class
},
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class Test {
@SlingObject
private Resource resource;
@OSGiService
private ResourceResolver resourceResolver;
@ValueMapValue
private String fileUrl;
@PostConstruct
public String getData() {
Resource resource = resourceResolver.getResource(fileUrl);
Resource parentProps = resource.getParent();
System.out.println("parent node is =>" + parentProps);
}
}
Is there a problem?有问题吗? My code builds correctly but does not return anything我的代码构建正确但不返回任何内容
ResourceResolver is not a OSGI service, its SlingObject ResourceResolver 不是 OSGI 服务,它的 SlingObject
Also you need to make sure that logged-in user making the request have right access setup for the file path您还需要确保发出请求的登录用户具有文件路径的正确访问权限设置
I suggest you get comfortable debugging code so you can understand exactly what is going on behind the scenes instead of just adding logging statements.我建议您熟悉调试代码,这样您就可以准确了解幕后发生的事情,而不仅仅是添加日志记录语句。 Also, not totally sure where System.out.println calls get logged, but you can find logging information in /crx-quickstar/logs/error.log using an slf4j logger.此外,不完全确定 System.out.println 调用在哪里被记录,但您可以使用 slf4j 记录器在 /crx-quickstar/logs/error.log 中找到日志信息。 What I do is configure the log level to ERROR on my local instance so that it's easier to find my logging statements我所做的是在本地实例上将日志级别配置为 ERROR,以便更容易找到我的日志记录语句
As Ameesh said, you need to use @SlingObject
to inject ResourceResolver, although you can just use the annotation @ResourcePath
which does all the resource resolution for you:正如 Ameesh 所说,您需要使用@SlingObject
来注入 ResourceResolver,尽管您可以使用注释@ResourcePath
为您完成所有资源解析:
@Getter
@Model(adaptables = Resource.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class Test {
private static final Logger LOG = LoggerFactory.getLogger(Test.class);
@Self
private Resource resource;
@ResourcePath(name = "fileUrl")
private Resource file;
@PostConstruct
public String getData() {
final String path = Optional.ofNullable(file)
.map(file -> file.getParent())
.map(parent -> parent.getPath())
.orElse("resource not found!");
LOG.error("parent node is: {}", path);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.