2013-11-15 100 views
3

我正在研究一個小型的Web應用程序,我面臨一個問題。在這裏,我有一個使用迭代器在jsp頁面中顯示的內容列表。對於每一個內容都可能有一些與之相關的圖像。我想要顯示這些圖像以及內容。這是如何實現的。使用struts2迭代器在jsp中顯示多個圖像

這裏是說我的內容迭代器。

<s:iterator value="contentList"> 
    <s:property value="id"/> 
    <s:property value="topic"/> 

現在在這個迭代器標籤中,我想用第一個迭代器的「id」屬性動態地生成第二個迭代器。我有一個由「id」屬性映射到每個內容的圖像數據庫表。我想獲取與特定內容相關的圖像列表,然後將它們顯示在jsp頁面中。圖像可以是多個。這怎麼能實現。請幫忙。

圖像存儲在文件系統中,路徑存儲在數據庫中。爲了顯示單個圖像,我正在做的是,我從動作類的數據庫中獲取圖像路徑,然後通過fileInputStream將圖像傳遞給jsp頁面。當我嘗試顯示多個圖像時出現問題。用於JSP

<img src="contentimage.action?contentID=<s:property value="id"/>"/> 

<s:property value="id"/>顯示圖像

代碼是可用的內容迭代的值。

而且在動作類

Image ImageObject=cd.getImageObject(contentID); 
File file = new File(ImageObject.getFilePath()+"/"+ImageObject.getFileName());   
FileInputStream fileInputStream = new FileInputStream(file); 

Image是一個Bean具有圖像類的屬性,如文件路徑在文件系統中,圖像文件名等 現在,對於給定contentID,也可以是多個圖像在數據庫中,全部使用不同的文件名和文件路徑。我可以將它們列入Image類型的列表中。現在如何在jsp頁面上迭代它們並顯示這些路徑的圖像。希望這一次我明確提出我的問題。它被映射到休眠

EDIT

我的內容類。

private int id; 
private String topic; 
//getters and Setters 

我的ContentImage類被映射到Hibernate。

private int contentId; // this is the id of the Content class 
private String fileName; //image file name 
private String filePath; // image file path, actual file is stored in file system 
private String imageByte; //Base64 string of the image file 
//getters and setters 

現在,在加載視圖jsp之前的動作類中,我得到了內容列表。

contentList=//got the list here; 

現在使用迭代器在jsp中顯示它。

<s:iterator value="contentList"> 
    <s:property value="id"/> 
    <s:property value="topic"/> 
    Now here i want a second iterator of my ContentImage class having a list of images corresponding to the id value of the contentList iterator. 
    //This is where i am having the problem. I can display single image with this: 
    <img src="contentimage.action?contentID=<s:property value="id"/>"/> //But i need to display all the images associated with this id. 

    So i need an iterator created dynamically here with the id attribute and then display the images. Like: 
    <s:iterator value="contentImageList"> 
     <img src="displayimage.action?filepath=<s:property value="filePath"/>"/> 
    </s:iterator> 
    </s:iterator> 

我無法創建第二個迭代器。請在這裏幫助我。

+0

我可以通過傳遞與「id」屬性的動作在「IMG」標籤的「SRC」字段中顯示的單個圖像。但有多個圖像。我怎樣才能用相同的「img」標籤顯示它們或迭代它們? –

+0

我不確定要明白你在問什麼。你能否提供你的bean來幫助我們理解? – Arthur

+0

將'img'標籤放入迭代器中。 –

回答

3

這裏有很多變量:數據庫中的圖像,還是文件系統中的圖像,只有路徑/ URI在數據庫中?你如何在頁面上顯示它們? Base64 data-URI Schemeouch沒有緩存)?你如何加載它們?在加載頁面之前的動作中,還是在迭代中使用例如<s:action />標記?

假設,你必須具有簡單屬性的對象和包含的Base64一個List<String>鹼情形中,從字節數組轉換圖像(與Apache共享的encodeBase64URLSafeString):

class Row { 
    private Long id; 
    private String topic; 
    private List<String> images; 
    /* GETTERS AND SETTERS */ 
} 

和加載這些對象的一個​​列表在行動,在到達視圖之前:

private List<Row> rows; 

/* GETTER AND SETTER */ 

public String execute(){ 
    rows = loadRowsInSomeWay(); 
    return SUCCESS; 
} 

然後在JSP中,你可以借鑑他們是這樣的:

<s:iterator value="rows"> 
    <s:property value="id"/> 
    <s:property value="topic"/> 

    <s:iterator value="images"> 
     <img src="data:image/jpeg;base64,<s:property/>"/> 
    </s:iterator> 
</s:iterator> 

編輯

假設你已經工作了一個形象,你有所謂的「圖像」像上面的結構,具有一定的屬性和字符串列表(或長,或其他)含圖像的id

<s:iterator value="rows"> 
    <s:property value="id"/> 
    <s:property value="topic"/> 

    <s:iterator value="images"> 
     <img src="contentimage.action?contentID=<s:property />"/>    
    </s:iterator> 
</s:iterator> 
+0

感謝您的回覆。我編輯了這個問題以提供更多信息。我會嘗試你的解決方案。直到現在我還沒有使用Base64方法來顯示圖像。我會嘗試。 –

+0

我已經嘗試了Base64編碼方法,並且圖像顯示正在工作。如果我以你的例子爲例,我有一個具有一些屬性的Row類。我無法在那裏創建一個List 屬性,因爲類被映射到hibernate,並且數據庫無法創建具有List參數的表列。我所做的是,創建另一個Bean類,將圖像映射到Row類的「id」(如外鍵)和其他必要圖像屬性的屬性「RowId」。現在我怎麼能在一個迭代器中得到這個列表,並在其他迭代器「行」中顯示列表 –

+0

你不能簡單地發佈你的代碼的相關部分?很難猜出缺失的部分:/順便說一句,你可以像上面那樣嵌套迭代器,對於你的情況是不夠的? –