我正在研究一個小型的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>
我無法創建第二個迭代器。請在這裏幫助我。
我可以通過傳遞與「id」屬性的動作在「IMG」標籤的「SRC」字段中顯示的單個圖像。但有多個圖像。我怎樣才能用相同的「img」標籤顯示它們或迭代它們? –
我不確定要明白你在問什麼。你能否提供你的bean來幫助我們理解? – Arthur
將'img'標籤放入迭代器中。 –