问题描述
我正在开发一个小型Web应用程序,但我遇到了问题.在这里,我有一个内容列表,我正在使用迭代器在jsp页面中显示该内容.对于每个内容,可能有一些与之相关的图像.我想将这些图像与内容一起显示.如何做到这一点.
i am working on a little web application where i am facing a problem. Here, i have a list of contents which i am displaying in a jsp page using an iterator. For each content there can be some images associated with it. I want to display those images along with the contents. How this can be achieved.
这里是我的内容迭代器.
Here is say my content iterator.
<s:iterator value="contentList">
<s:property value="id"/>
<s:property value="topic"/>
现在在此迭代器标签中,我想使用第一个迭代器的"id"属性动态生成第二个迭代器.我有一个通过"id"属性映射到每个内容的图像数据库表.我想获取与该特定内容关联的图像列表,然后在jsp页面中显示它们.图像可以是多个.如何做到这一点.请帮忙.
Now within this iterator tag, i want to generate a second iterator dynamically with the "id" attribute of the first iterator. I have a database table of images mapped to each content by the "id" attribute. I want to get the list of images associated with that particular content and then display them in the jsp page. The images can be multiple. How can this be achieved. Please help.
图像存储在文件系统中,路径存储在数据库中.为了显示单个图像,我正在做的是,我从动作类中的数据库获取图像路径,然后将图像通过fileInputStream传递到jsp页面.当我尝试显示多个图像时会出现问题.
The images are stored in file system, with the paths stored in database. For displaying a single image, what i am doing is that, i get the image path from the database in the action class and then pass the image by a fileInputStream to the jsp page. The problem arises when i try to display multiple images.
用于在jsp中显示图像的代码
Code used for displaying the image in jsp
<img src="contentimage.action?contentID=<s:property value="id"/>"/>
<s:property value="id"/>
是内容迭代器可用的值.
The <s:property value="id"/>
is the value available with the content iterator.
在行动课中
Image ImageObject=cd.getImageObject(contentID);
File file = new File(ImageObject.getFilePath()+"/"+ImageObject.getFileName());
FileInputStream fileInputStream = new FileInputStream(file);
Image
是具有图像属性的Bean类,例如文件系统中的FilePath,图像文件名等.现在,对于给定的contentID
,数据库中可以有多个图像,所有图像都具有不同的文件名和文件路径.我可以在Image
类型的列表中找到它们.现在,如何在jsp页面中对其进行迭代并显示这些路径的图像.希望我这次能澄清我的问题.
The Image
is a Bean class having image attributes, like FilePath in the file system, image file name etc.Now, for a given contentID
, there can be multiple images in the database, all with diffrent filename and filepath. I can get them in a list of Image
type. Now how to iterate over them in jsp page and display the images of those paths. Hope, i am making my question clear this time.
编辑
映射到Hibernate的我的内容类.
My Content Class which is mapped to Hibernate.
private int id;
private String topic;
//getters and Setters
我的ContentImage类,它映射到了Hibernate.
My ContentImage class which is mapped to 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之前的动作类中,我获得了内容列表.
Now, in the action class prior to loading the view jsp, i get the Content List.
contentList=//got the list here;
现在使用迭代器在jsp中显示它.
Now displaying it in the jsp using an iterator.
<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>
我无法创建第二个迭代器.请为此提供帮助.
I am not being able to create the second iterator.Please help me in this.
推荐答案
这里有很多变量:数据库中的图像还是文件系统中的图像,而数据库中只有路径/URI?您如何在页面上显示它们? Base64数据URI方案( ouch 无需缓存)?您如何加载它们?在加载页面之前的操作"中,或在带有<s:action />
标签?
There are a lot of variables here: are the images in the database, or in a File System and only the path / URI is in the database ? How do you display them on page ? Base64 data-URI Scheme (ouch for no caching)? How do you load them ? In the Action before the page is loaded, or in the iteration with for example <s:action />
tag ?
假设有一个基本方案,其中您有一个具有简单属性的对象,一个List<String>
包含经过Base64转换的图像(使用Apache Commons的 encodeBase64URLSafeString ):
Assuming a base scenario where you have an object with simple properties and a List<String>
containing the Base64 converted images (with Apache Commons's encodeBase64URLSafeString) from the byte arrays:
class Row {
private Long id;
private String topic;
private List<String> images;
/* GETTERS AND SETTERS */
}
,然后在到达视图之前,在操作中加载这些对象的列表:
and you load a List of these objects in the action, before reaching the view:
private List<Row> rows;
/* GETTER AND SETTER */
public String execute(){
rows = loadRowsInSomeWay();
return SUCCESS;
}
然后在JSP中可以这样绘制它们:
then in JSP you could draw them like this:
<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
的名为"images"的String(或Long或其他名称)列表:
Assuming what you have already works for one image, and that you have a structure like above, with some properties and a list of String (or Long, or whatever) called "images" containing the id
of the images:
<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>
这篇关于使用struts2迭代器在jsp中显示多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!