小编典典

使用struts2迭代器在jsp中显示多个图像

jsp

我正在处理一个面临问题的小型Web应用程序。在这里,我有一个内容列表,我正在使用迭代器在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页面中对其进行迭代并显示这些路径的图像。希望我这次能澄清我的问题。

编辑

我的内容类已映射到Hibernate。

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>

我无法创建第二个迭代器。请为此提供帮助。


阅读 240

收藏
2020-06-10

共1个答案

小编典典

这里有很多变量:图像是在数据库中还是在文件系统中,并且只有路径/
URI在数据库中?您如何在页面上显示它们?Base64编码数据URI方案
哎哟 对于没有缓存)?您如何加载它们?在页面加载之前的Action中,或在带有例如<s:action />tag 的迭代中?

假设您拥有一个具有简单属性的对象,并且该对象List<String>包含字节数组中的Base64转换图像(使用Apache
Commons的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>

编辑

假设您已经对一个图像进行了操作,并且具有类似上面的结构,并且具有一些属性和一个称为“
images”的String(或Long或其他内容)列表,其中包含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>
2020-06-10