在项目中 我们经常会碰到要输出图片以及查询结果list到页面jsp的情况,单纯的只输出list到jsp倒是很简单。但是要将2者同时输出 ,可能就有点麻烦。
不知道webwork是否提供了这方面的支持。
在这里,我们就用最土的办法来做了。
首先,写1个OutListAction,它有2个方法,一个是getImage(),另一个是getList()。OutListAction extends WebWorkResultSupport 这样子,action就可以return null 了。return null的目的是为了采用response输出图片流.
OK! Action写完了!现在我们来看list.jsp
这里要输出图片的话,通过javascript来获取该图片输出流。其代码如下:
至于list输出就随便输出了!
不知道webwork是否提供了这方面的支持。
在这里,我们就用最土的办法来做了。
首先,写1个OutListAction,它有2个方法,一个是getImage(),另一个是getList()。OutListAction extends WebWorkResultSupport 这样子,action就可以return null 了。return null的目的是为了采用response输出图片流.
- publicclassOutListActionextendsWebWorkResultSupport{publicStringgetImage()throwsIOException{
- InputStreamin=null;
- OutputStreamout=ServletActionContext.getResponse().getOutputStream();
- ServletActionContext.getResponse().setContentType("image/jpeg");
- StringstrFullPath=ServletActionContext.getServletContext()
- .getRealPath("/");
- Filef=newFile(strFullPath+"img//none.bmp");
- in=newFileInputStream(f);//初始化inputStream默认为img//none.bmp
- if(picno!=null&&!picno.equals("")){
- imageList=dao.getImageByID(picno);
- if(imageList!=null&&imageList.size()>0){
- Imageimg=(Image)imageList.get(0);
- if(img!=null&&img.getImage()!=null){
- Blobblob=img.getImage();//上面这部分都是通过picno来查询数据中是否有该图片,如果没,就采用默认的图片img//none.bmp来显示在页面。
- if(blob!=null){
- try{
- in=blob.getBinaryStream();//更改inputStream
- }catch(SQLExceptione){
- e.printStackTrace();
- }
- }
- }
- }
- }
- try{
- byte[]b=newbyte[1024];
- inti=0;
- while((i=in.read(b))>0){
- out.write(b,0,i);//读图片
- }
- }catch(Exceptione){
- e.printStackTrace();
- }finally{
- if(in!=null){
- in.close();
- if(out!=null){
- out.close();
- }
- }
- }
- returnnull;
- }
- publicStringgetList(){
- kindList=dao.getKindName();
- }
- }
OK! Action写完了!现在我们来看list.jsp
这里要输出图片的话,通过javascript来获取该图片输出流。其代码如下:
- <imgid="carimage"width="135"height="120"hspace="2"></td>
- <scripttype="text/javascript">
- varpicno='<ww:propertyvalue="top[37]"/>';
- varurl="getImage.action?picno="+picno;
- document.getElementById("carimage").src=url;
- </script>
至于list输出就随便输出了!
- <ww:iteratorvalue="kindList"status="li">
- <ww:propertyvalue="#li.count"/>
- </ww:iterator>