2013-11-15 102 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

你不能简单地发布你的代码的相关部分?很难猜出缺失的部分:/顺便说一句,你可以像上面那样嵌套迭代器,对于你的情况是不够的? –