gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:16 26 4
gpt4 key购买 nike

我正在开发一个小型 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"/>是内容迭代器可用的值。

在 Action 类中

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

Image是一个具有图片属性的Bean类,如文件系统中的FilePath,图片文件名等。现在,对于给定的 contentID ,数据库中可以有多个图像,所有图像都具有不同的文件名和文件路径。我可以在 Image 的列表中找到它们类型。现在如何在 jsp 页面中迭代它们并显示这些路径的图像。希望这次我能把我的问题说清楚。

编辑

映射到 Hibernate 的我的内容类。

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

映射到 Hibernate 的我的 ContentImage 类。

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

现在,在加载 View 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>

我无法创建第二个迭代器。请帮助我。

最佳答案

这里有很多变数:图像是在数据库中,还是在文件系统中,数据库中只有路径/URI?你如何在页面上显示它们? Base64 data-URI Scheme (哎呀 没有缓存)?你如何加载它们?在加载页面之前的操作中,或者在迭代中,例如 <s:action />标签?

假设一个基本场景,您有一个具有简单属性和 List<String> 的对象包含来自字节数组的 Base64 转换图像(使用 Apache Commons 的 encodeBase64URLSafeString ):

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

然后在到达 View 之前在操作中加载这些对象的列表:

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”的字符串(或 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>

关于java - 使用struts2迭代器在jsp中显示多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19995290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com