如何在Struts 2中显示图像? [英] How to display image in Struts 2?

查看:175
本文介绍了如何在Struts 2中显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检索数据库中的行,并将图像保存在字节数组中。如何使用Strut2显示图像?



我使用自定义结果显示它。我通过了id,并在数据库中搜索一个动作。我的问题是如何调用这几次?我有一个操作,检索所有的行。如何从列表操作调用显示图像操作?



我的行动有一个我想在JSP上显示的对象列表。



JSP



第一种方式:我使用迭代器标签,在每次迭代我想显示的图像是在字节数组。



第二种方法:我试过这个 post ,它对我来说不起作用。

 < img src =data:image / jpg; base64,< s:property value =imageData>< / s:property> /& > 

图像数据是字符串中的图像。

解决方案

要在数据库中检索行有很多方法,但是他们都使用JDBC API。您可以将对象映射为包含您在数据库中持久存在的字节或blob或clob数据的图像数组。显示将其发送到浏览器所需的图像。在Struts2中有一个流结果类型,可用于输出图像数据到浏览器。或者使用 Servlet API 在JSP上传输数据,类似于< a href =http://stackoverflow.com/a/15278402/573032>如何发送和显示图像从动作类到JSP在Struts 2的答案。将您已经在该问题中启发的答案的经验与字符串化数据相结合,可以获得合适的结果。


I need to retrieve rows in database with a image saved in byte array. How do I display the image using Strut2?

I have displayed it by using custom result. I passed the id and make an action search for it in the database. My problem is how to invoke this several times? I have an action that retrieve all the rows. How to call display image action from the list action? Or is there a better way to achieve this?

JAVA

My action has an list of the objects I want to show on JSP.

JSP

First way: I use iterator tag and on each iteration I want to display the image which is in byte array. I want call the action to retrieve the image here.

Second way: I tried this post and it is not working for me.

<img src="data:image/jpg;base64, <s:property value="imageData"></s:property> />" >

Image data is the image in string.

解决方案

To retrieve rows in database there's many ways but all of them utilize JDBC API. You can map the object as an image array containing bytes or blob or clob data that you persist in the database. Displaying the images required to send it to the browser. In the Struts2 there's a stream result type that could be used to output the image data to the browser. Or use the Servlet API to transmit the data on the JSP similar like in How to send and display image from action class to JSP in Struts 2 answer. Combining the experience from the answers that you already enlighten in the question to stringify data you may achieve suitable results.

这篇关于如何在Struts 2中显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆