2012-07-27 72 views
0

要求:我想在jsp頁面上顯示圖像的詳細信息(imageName,imagesize,圖像)。圖像與對象類型blob一起存儲在數據庫中。數據庫表的列是imageName(varchar2),imageSize(number),image(blob)。 Jsp'Show Images'中有一個按鈕。在點擊這個時,我必須以表格形式顯示數據庫表中所有圖像的圖像名稱和圖像大小。 摘要:點擊顯示圖像時,我必須顯示所有帶有圖像名稱的圖像,這些圖像存儲在數據庫中。在Jsp上顯示所有圖像的名稱和大小

技術:struts2中,JSP,JDBC

我做了什麼:我能看到JSP,但無法顯示圖像名稱和大小

BlobRetrieval.jsp

<%@page import="java.sql.Blob"%> 
<%@page import="java.io.OutputStream"%> 
<%@page import="java.sql.SQLException"%> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@ taglib prefix="s" uri="/struts-tags" %> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Esp Page</title> 
    <script type="text/javascript"> 
     function showImage() 
     { 
      document.imgFrm.action="step1Image"; 
      document.imgFrm.submit(); 
     } 
    </script> 
</head> 
<body>  
    <s:form name="imgFrm"> 
     <input type="button" name="b" value="ShowImage" onclick="showImage()"/> 
    <% System.out.println("[BlobRetrieval.jsp] session value "+session.getAttribute("imageRetrieved")+" if null then dont show images");%> 
     <% 
     if(session.getAttribute("imageRetrieved")!=null) 
     { 
     %> 
     <br></br> 
     <table id="myDiv" name="myDiv" style="color: #0900C4; font: Helvetica 12pt;border: 1px solid black; height: auto;"> 
      <tr> 
       <td>Image name</td> 
       <td> <img src="DisplayBlob.jsp"> </img></td> 
      </tr> 
     </table> 
     <% 
     } 
    else 
    { 
System.out.println("[BlobRetrieval.jsp]Session value is null"); 
    } 
%> 
    </s:form> 
</body> 
</html> 
圖像

DisplayBlob.jsp

<%@ page import="java.sql.*"%> 

<%@ page import="java.io.*"%> 

<% Blob image = null; 

java.sql.Connection con = null; 

byte[ ] imgData = null ; 

java.sql.Statement stmt = null; 

java.sql.ResultSet rs = null; 

try { 



    con = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@ip:port:sid","schemaName", "password"); 

stmt = con.createStatement(); 

rs = stmt.executeQuery("SELECT fileobj,fileName,fileSize FROM DISPLAYBLOB"); 

if (rs.next()) { 

image = rs.getBlob(1); 

imgData = image.getBytes(1,(int)image.length()); 

} else { 

out.println("Display Blob Example"); 

out.println("image not found for given id>"); 

return; 

} 

// display the image 

response.setContentType("image/gif"); 

OutputStream o = response.getOutputStream(); 

o.write(imgData); 

o.flush(); 

o.close(); 
} catch (Exception e) { 

out.println("Unable To Display image"); 

out.println("Image Display Error=" + e.getMessage()); 

return; 

} finally { 

try { 

rs.close(); 

stmt.close(); 

con.close(); 

} catch (SQLException e) { 

e.printStackTrace(); 

} 

} 

%> 

struts.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
"http://struts.apache.org/dtds/struts-2.0.dtd"> 
<struts> 
<constant name="struts.devMode" value="true" /> 
<package name="struts2" extends="struts-default" namespace="/"> 
    <action name="*Image" class="example.DisplayBlob" method="{1}"> 
     <result name="input">BlobRetrieval.jsp</result> 
    </action> 
</package> 
<constant name="struts.action.excludePattern" value="/*.servlet"/> 
</struts> 

DisplayBlob.java

package example; 
import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.ActionSupport; 
import java.util.Map; 
public class DisplayBlob extends ActionSupport { 
public String step() throws Exception 
{ 
    Map session = (Map)ActionContext.getContext().getSession(); 
    session.clear(); 
    System.out.print("[DisplayBlob] step "); 
    return "input"; 
} 
public String step1() throws Exception 
{ 
    Map session = (Map)ActionContext.getContext().getSession(); 
    System.out.print("[DisplayBlob] step1 "); 
    session.put("imageRetrieved", "imageRetrieved"); 
    return "input"; 
} 
} 

首先我正在訪問的URL IP:端口/上下文根/ stepImage 有關訪問該URL我打電話 'DisplayBlob.java',該轉發控制到BlobRetrieval的步驟furntion .jsp,然後點擊'show images'按鈕,我調用'Displayblob.java'的step1的函數,該函數再次將控制轉發給'BlobRetrieval.jsp'。由於此時在會話中設置了'imageRetrieved'值,所以控制進入if循環。在if循環中,我使用圖像src標籤顯示圖像,但無法顯示其他字段的「圖像名稱」和「圖像大小」。如果在db中存儲了多個圖像,該怎麼辦?那麼我的代碼應該是什麼樣子? 我不能認爲imagename應該如何來自db?如果沒有任何事件(用戶投訴),我將如何顯示所有imageNames圖像。

回答

2

我想在這裏提到的幾件事情是,雖然你編碼的工作方式將解決你的問題,但在未來的代碼更改維護將真的會成爲一場噩夢。

Regardiing你的問題,我建議你創建屬性DTO /豆你需要在UI

public class ImageBean{ 
private String imageName; 
private int imageSize 
    // any other field you want to 
    //there getter and setters 

} 

展現我強烈建議你從你的JSP代碼移出數據庫連接代碼,然後移動它到一個類說ConnectionManager應該負責創建/建立與數據庫的連接。 創建其他類應該執行您的SQL查詢,並會給你javalist的結果

在你動作類調用該類將返回列表存儲在您的數據庫列表中的所有圖像,你可以發回列表返回在JSP像

public class ShowImageAction extends ActionSupport{ 

    private List<ImageBean> imageList; 
    // getter setters for this 

    public String showAllImages() throws Exception{ 
     imageList=ConnectionManager.getConnection.executeQuery("fetch all images from DB");  
    } 
} 

在你的JSP時,在節目中的所有圖像按鈕,你需要調用的ShowImageActionshowAllImages方法用戶點擊。使用迭代器在JSP來顯示所有圖像被獲取從DB

<s:iterator value="imageList" status="imgObject"> 
    <s:property value="imageName" /> 
    <s:property value="imageSize" /> 
</s:iterator> 

用於顯示圖像可以存儲每個圖像的唯一ID和點擊時可以通過該ID的動作和基於ID可以獲取圖像。

對於顯示圖像,你可以使用Struts2的stream result型像

<result name="selectedImage" type="stream"> 
       <param name="contentType">image/jpg</param> 
       <param name="inputName">imageStream</param> 
       <param 
name="contentDisposition">filename="image.jpg"</param> 
       <param name="bufferSize">1024</param> 
      </result> 
+0

@ Umesh..Thankx的答覆!我會照顧你的所有建議......「爲了顯示圖像,你可以存儲每個圖像的唯一ID,點擊後可以將該ID傳遞給動作,並根據ID獲取圖像。」沒有點擊,這是我的問題。沒有任何點擊我怎麼能顯示所有圖像..一個解決方案是第一我retieve所有imageName和存儲在ArrayList ....然後迭代此aray列表,然後迭代時將每個imageName傳遞給databaase和檢索blob(再次訪問數據庫)...我無法理解struts2的流結果,你能指導我任何示例嗎? – user752590 2012-07-27 12:10:39

+1

@ user752590你需要什麼具體的例子? [流結果文檔](http://struts.apache.org/2.x/docs/stream-result.html)顯示了配置。在'struts-filedownload'部分的展示應用程序中有一個例子。 – 2012-07-27 16:38:09

相關問題