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


在项目中 我们经常会碰到要输出图片以及查询结果list到页面jsp的情况,单纯的只输出list到jsp倒是很简单。但是要将2者同时输出 ,可能就有点麻烦。
不知道webwork是否提供了这方面的支持。
在这里,我们就用最土的办法来做了。

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

Java代码
收藏代码
  1. publicclassOutListActionextendsWebWorkResultSupport{publicStringgetImage()throwsIOException{
  2. InputStreamin=null;
  3. OutputStreamout=ServletActionContext.getResponse().getOutputStream();
  4. ServletActionContext.getResponse().setContentType("image/jpeg");
  5. StringstrFullPath=ServletActionContext.getServletContext()
  6. .getRealPath("/");
  7. Filef=newFile(strFullPath+"img//none.bmp");
  8. in=newFileInputStream(f);//初始化inputStream默认为img//none.bmp
  9. if(picno!=null&&!picno.equals("")){
  10. imageList=dao.getImageByID(picno);
  11. if(imageList!=null&&imageList.size()>0){
  12. Imageimg=(Image)imageList.get(0);
  13. if(img!=null&&img.getImage()!=null){
  14. Blobblob=img.getImage();//上面这部分都是通过picno来查询数据中是否有该图片,如果没,就采用默认的图片img//none.bmp来显示在页面。
  15. if(blob!=null){
  16. try{
  17. in=blob.getBinaryStream();//更改inputStream
  18. }catch(SQLExceptione){
  19. e.printStackTrace();
  20. }
  21. }
  22. }
  23. }
  24. }
  25. try{
  26. byte[]b=newbyte[1024];
  27. inti=0;
  28. while((i=in.read(b))>0){
  29. out.write(b,0,i);//读图片
  30. }
  31. }catch(Exceptione){
  32. e.printStackTrace();
  33. }finally{
  34. if(in!=null){
  35. in.close();
  36. if(out!=null){
  37. out.close();
  38. }
  39. }
  40. }
  41. returnnull;
  42. }
  43. publicStringgetList(){
  44. kindList=dao.getKindName();
  45. }
  46. }

OK! Action写完了!现在我们来看list.jsp
这里要输出图片的话,通过javascript来获取该图片输出流。其代码如下:
Java代码
收藏代码
  1. <imgid="carimage"width="135"height="120"hspace="2"></td>
  2. <scripttype="text/javascript">
  3. varpicno='<ww:propertyvalue="top[37]"/>';
  4. varurl="getImage.action?picno="+picno;
  5. document.getElementById("carimage").src=url;
  6. </script>

至于list输出就随便输出了!
Java代码
收藏代码
  1. <ww:iteratorvalue="kindList"status="li">
  2. <ww:propertyvalue="#li.count"/>
  3. </ww:iterator>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值