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

来源:互联网 发布:知乎石油出口禁令 编辑:程序博客网 时间:2024/06/02 05:25


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

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

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

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

至于list输出就随便输出了! 
Java代码 
 收藏代码
  1. <ww:iterator value="kindList " status="li">  
  2. <ww:property value="#li.count" />  
  3. </ww:iterator >  
原创粉丝点击