我正在研究一个小型的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'标签放入迭代器中。 –