2013-10-03 71 views
1

我是CQ5的新手,我遇到了以下問題。CQ5 - 如何獲取頁面圖片

我試圖顯示一個列表與子頁面後面的圖像。我設法讓列表本身工作,但圖像出現問題,路徑不正確。

到目前爲止,我已經有了這個

<div class="img-list"> 
    <img src="${list.img}" /> 
</div> 

public class ImageList { 
private String img = ""; 
private Page listItem; 
String extension; 
Resource r; 

public ImageList(Page p) { 
    this.listItem = p; 
    this.r = listItem.getContentResource("image"); 

} 

public String getImg() { 

    if (r != null) { 
     //Image image = new Image(r); 
     img = r.getPath(); 
    } 

    return img; 
} 

public void setImg (String i) { 
    this.img = i; 
} 

} 

但是,這是行不通的。有人可以幫我嗎?

在此先感謝

編輯,得到了它與下面的解決方案工作:

public String getImg() { 
    String imagePath = ""; 
    if (r != null) { 
     Image image = new Image(r); 
     imagePath = (String)listItem.getPath()+".img.png"+ image.getSuffix(); 
    } 
    return imagePath; 
} 

回答

1

如果我理解正確,您正嘗試從子頁面獲取圖像列表。

爲什麼不嘗試創建一個對象,它映射到當前頁面(實際上是一個當前頁面的對象),然後使用listChildren方法,列出所有的子頁面。然後你可以使用page.getContentResorce(「圖像」)方法,創建一個圖像對象,然後再返回與每個子頁面相關的圖像的路徑..

事情是這樣的: -

Iterator<Page> childPageIterator = Currentpage.listChildren(new PageFilter(request)); 
while(pageIterator.hasNext()) 
      { 
       Page childPage = pageIterator.next(); 
         Resource r = page.getContentResource("image"); 
         if (r != null) { 
          Image image = new Image(r); 
          String imagePath = (String)contentpage.getPath()+".img.png"+ image.getSuffix(); 
         } 
      } 

你可能需要根據您的需要量身定做,但希望這可以讓您開始

+0

讓孩子不是問題。問題是顯示圖像。我剛剛嘗試了一些您的解決方案的修改版本,輸出爲。另外,我並不想從當前頁面獲取孩子。我正在使用不同頁面的孩子。 –

+0

Nevermind,發了一個小的錯字,我剛剛使用了String imagePath =(String)contentpage.getPath()+「。img.png」+ image.getSuffix();在我原來的解決方案,它的工作,謝謝! –

0

您正在試圖獲取資源(圖像節點)的路徑,並提供給的src屬性圖像標籤,而不是提供圖像的實際路徑。

通過如下圖所示

public String getImg() { 
    if (r != null) { 
     Node node = r.adaptTo(Node.class); 
     if(node.hasProperty("fileReference"){ 
      img = node.getProperty("fileReference").getString(); 
     } 
    } 
    return img; 
} 

或者你可以使用com.day.cq.wcm.foundation.Image類通過繪製圖像改變GETIMG()方法接近的一種方式傳遞資源對象,而不是使用標籤。

+0

感謝您的回覆!但是當我這樣做時,輸出是,沒有更多。我確信這些子頁面上有一張圖片。有任何想法嗎? –

相關問題