您的位置:首页 > Web前端 > JavaScript

webwork action同时输出图片以及其他数据信息到jsp

2013-11-28 15:52 465 查看



在项目中 我们经常会碰到要输出图片以及查询结果list到页面jsp的情况,单纯的只输出list到jsp倒是很简单。但是要将2者同时输出 ,可能就有点麻烦。

不知道webwork是否提供了这方面的支持。

在这里,我们就用最土的办法来做了。

首先,写1个OutListAction,它有2个方法,一个是getImage(),另一个是getList()。OutListAction extends WebWorkResultSupport 这样子,action就可以return null 了。return null的目的是为了采用response输出图片流.

Java代码




public class OutListAction extends WebWorkResultSupport {public String getImage() throws IOException {

InputStream in = null;

OutputStream out = ServletActionContext.getResponse().getOutputStream();

ServletActionContext.getResponse().setContentType("image/jpeg");

String strFullPath = ServletActionContext.getServletContext()

.getRealPath("/");

File f = new File(strFullPath + "img//none.bmp");

in = new FileInputStream(f);// 初始化inputStream 默认为img//none.bmp

if (picno != null && !picno.equals("")) {

imageList = dao.getImageByID(picno);

if (imageList != null && imageList.size() > 0) {

Image img = (Image) imageList.get(0);

if (img != null && img.getImage() != null) {

Blob blob = img.getImage();//上面这部分都是通过picno来查询数据中是否有该图片,如果没,就采用默认的图片img//none.bmp来显示在页面。

if (blob != null) {

try {

in = blob.getBinaryStream(); // 更改inputStream

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

}

try {

byte[] b = new byte[1024];

int i = 0;

while ((i = in.read(b)) > 0) {

out.write(b, 0, i); // 读图片

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if (in != null) {

in.close();

if (out != null) {

out.close();

}

}

}

return null;

}

public String getList() {

kindList = dao.getKindName();

}

}

OK! Action写完了!现在我们来看list.jsp

这里要输出图片的话,通过javascript来获取该图片输出流。其代码如下:

Java代码




<img id ="carimage" width="135" height="120" hspace="2"></td>

<script type = "text/javascript">

var picno ='<ww:property value="top[37]" />';

var url ="getImage.action?picno="+picno;

document.getElementById("carimage").src=url;

</script>

至于list输出就随便输出了!

Java代码




<ww:iterator value="kindList " status="li">

<ww:property value="#li.count" />

</ww:iterator >
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐