问题描述
一旦用户在cq对话框中提供了文件路径,我就需要获取父文件夹名称(字符串类型)。这是我的方法:
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);
}
}
解决方法
ResourceResolver不是OSGI服务,它的SlingObject
还需要确保发出请求的登录用户具有正确的文件路径访问权限设置
,我建议您获得舒适的调试代码,以便您可以准确了解幕后情况,而不仅仅是添加日志记录语句。同样,也不能完全确定System.out.println调用的记录位置,但是您可以使用slf4j记录器在/crx-quickstar/logs/error.log中找到记录信息。我要做的是在本地实例上将日志级别配置为ERROR,以便更轻松地找到日志记录语句
正如Ameesh所说,尽管您可以只使用注释@Injectable({
providedIn: 'root',})
来为您完成所有资源解析,但是您需要使用@SlingObject
来注入ResourceResolver:
@ResourcePath