webwork action同時輸出圖片以及其他數據信息到jsp

在項目中 我們經常會碰到要輸出圖片以及查詢結果list到頁面jsp的情況,單純的只輸出list到jsp倒是很簡單。但是要將2者同時輸出 ,可能就有點麻煩。
不知道webwork是否提供了這方面的支持。
在這裏,我們就用最土的辦法來做了。

首先,寫1個OutListAction,它有2個方法,一個是getImage(),另一個是getList()。OutListAction extends WebWorkResultSupport 這樣子,action就可以return null 了。return null的目的是爲了採用response輸出圖片流.

[code]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();
}
}
[/code]
OK! Action寫完了!現在我們來看list.jsp
這裏要輸出圖片的話,通過javascript來獲取該圖片輸出流。其代碼如下:
[code]
<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>
[/code]
至於list輸出就隨便輸出了!
[code]
<ww:iterator value="kindList " status="li">
<ww:property value="#li.count" />
</ww:iterator >
[/code]

好了,基本上就這樣! 不知道大家有沒有更好更簡單的方法,請指正!
發佈了6 篇原創文章 · 獲贊 1 · 訪問量 3241
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章