我正在处理名为" tabbedcarousel"的旋转木马组件,我需要遍历它的子节点才能访问图像属性。见下图:
" tabbedimagecarouselunselectedimage" - > "一个FileReference"是我需要得到的。
说我正在制作" tabbedcarousel.jsp",我可以访问我的currentNode,所以我假设获得深度子img属性的方式是这样的:
<%
NodeIterator tabbedCarouselChildNodes = currentNode.getNodes();
while(tabbedCarouselChildNodes.hasNext()){
Node parNode = tabbedCarouselChildNodes.nextNode();
NodeIterator parChildNodes = parNode.getNodes();
while(parChildNodes.hasNext()){
Node tabbedCarouselItemNode = parChildNodes.nextNode();
NodeIterator tabbedCarouselItemChildNodes = tabbedCarouselItemNode.getNodes();
while(tabbedCarouselItemChildNodes.hasNext()){
Node tabImgNode = tabbedCarouselItemChildNodes.nextNode();
%>
test here s the img property : <%= tabImgNode.getProperties("fileReference").toString()%> !!!
<%
}
}
%>
但它不起作用。希望有人可以给我一些建议。感谢
答案 0 :(得分:8)
我在最后添加了一个大括号,并将tabImgNode.getProperties()
替换为hasProperty()
/ getProperty()
组合。在我的CQ上工作正常:
<%
NodeIterator tabbedCarouselChildNodes = currentNode.getNodes();
while(tabbedCarouselChildNodes.hasNext()) {
Node parNode = tabbedCarouselChildNodes.nextNode();
NodeIterator parChildNodes = parNode.getNodes();
while(parChildNodes.hasNext()){
Node tabbedCarouselItemNode = parChildNodes.nextNode();
NodeIterator tabbedCarouselItemChildNodes = tabbedCarouselItemNode.getNodes();
while(tabbedCarouselItemChildNodes.hasNext()){
Node tabImgNode = tabbedCarouselItemChildNodes.nextNode();
if (!tabImgNode.hasProperty("fileReference")) {
continue;
}
%>
test: <%= tabImgNode.getProperty("fileReference").getString()%>
<%
}
}
}
%>
BTW,可以使用Sling API重构:
<%
Iterator<Resource> items = resource.getChild("par").listChildren();
while (items.hasNext()) {
Resource property = items.next().getChild("tabimageunselectedimage/fileReference");
if (property == null) {
continue;
}
%>
fileReference: <%= property.adaptTo(String.class) %>
<% } %>
第三种选择是使用名为SlingQuery的第三方库:
<% for (Resource r : SlingQuery.$(resource).find("#tabimageunselectedimage")) { %>
path: <%= r.adaptTo(ValueMap.class).get("fileReference") %>
<% } %>
答案 1 :(得分:0)
遇到这个因为我一直在寻找必须有效的方法来检索给定起始节点的任何深度的jcr节点。在你的情况下,我将使用递归来获得期望的结果。
在OSGI服务中
public class WriteImageNode{
private SlingHttpServletRequest slingRequest;
private SlingScriptHelper sling;
private ArrayList<String, String> tabImageList = new ArrayList<String, String>();
public WriteImageNode( SlingHttpServletRequest slingRequest, SlingScriptHelper sling){
this.slingRequest = slingRequest;
this.sling = sling;
}
public ArrayList<String> getTabImageNode(String path){
Session currentSession = null;
SlingRepository repo ;
if(!path.equals("")){
Node startNode = slingRequest.getResourceResolver().getResource(path).adaptTo(Node.class);
try {
repo = sling.getService(SlingRepository.class);
currentSession = repo.loginAdministrative(null);
NodeIterator iterator = startNode.getNodes();
while(iterator.hasNext()){
Node childNode = iterator.nextNode();
if(childNode.hasProperty("fileReference")){
tabImageList.add(childNode.getProperty("fileReference").getString());
}
else{
getTabImageNode(childNode.getPath());
}
}
} catch (RepositoryException e) {
//do something clever with the exception
} finally {
if (currentSession != null) {
currentSession.logout();
}
}
}
}
return tabImageList;
}
在你的jsp中做这样的事情:
<%
request.setAttribute("tabImageList", new WriteImageNode(slingRequest, sling).getTabImageNode(path));
%>
<c:forEach var="imagelist" items="${tabImageList}">
<c:out value="${imagelist}/>
</c:forEach>