JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Object Relational Mapping » Hibernate  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Blob在jsp顯示圖片的問題
小遲





發文: 4
積分: 0
於 2015-10-25 06:24 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
寫入資料庫都ok,
我的寫入post方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Session session = HibernateUtil.getSessionFactory().openSession();
    ProductHibernateDAO cc = new ProductHibernateDAO();
    request.setCharacterEncoding("utf-8");
    String pn = request.getParameter("proNam");
    String pc = request.getParameter("proCon");
    String pp = request.getParameter("proPri");
    double dpp = Double.parseDouble(pp);
    String ps = request.getParameter("proSto");
    int ips = Integer.parseInt(ps);
    
    Part part = request.getPart("productFile");
    long size = part.getSize(); 
    InputStream is =part.getInputStream();
    Blob sb = Hibernate.getLobCreator(session).
        createBlob(is, size);
    Product product = new Product(1, pn, pc, 
        dpp, ips, sb);
    cc.save(product);
    String pf = request.getParameter("productFile");


但blob部分,網頁上面卻顯示一張小圖(附件),不知怎麼會這樣。
這是我的productbean:
1
2
3
4
5
6
7
private long productNo;
String productName;
String productContent;
double productPrice;
int productStock;
private Blob productPic;
其他就是get,set方法

我的hibernatedao方法:就是我等等用到的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public List<Product> getAll() {
    SessionFactory factory = HibernateUtil.getSessionFactory();
    Session session = factory.openSession();
    Transaction tx = null;
    List<Product> list = null;
    try {
      tx = session.beginTransaction();
      Query query = session.createQuery("from Product"); 
      list = (List<Product> query.list();
      tx.commit();
    } catch(Exception ex){
      if (tx != null) tx.rollback();
      System.out.println(ex.getMessage());
    } 
    return list;
  }

1
2
3
4
5
6
7
8
9
10
11
12
我的servlet:
  protected void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("image/gif");
     ProductHibernateDAO ch = new ProductHibernateDAO();
     List<Product> list = ch.getAll();
     request.setAttribute("allProduct", list);
     RequestDispatcher rd = request.getRequestDispatcher
     ("showCart.jsp");
     rd.forward(request, response);
     return;
  }

最後則是showCart.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
        <c:if test='${not empty allProduct}'>
          <c:forEach var='mb' varStatus='vs' items='${allProduct}'>
            <c:if test='${vs.first}'>
              <c:out value="" escapeXml='false' />
              <c:out
                value="<tbody><tr class='warning'>
<td></td><td>編號</td>
          <td>帳號</td><td>密碼</td><td>email</td><td></td></tbody>"
                escapeXml='false' />
            </c:if>
            <tbody>
              <tr>
 
                <td>${mb.productNo}</td>
                <td>${mb.productName}</td>
                <td>${mb.productContent}</td>
                <td>${mb.productPrice}</td>
                <td>${mb.productStock}</td>
  //就是這個撈不出來------------------->>  <td><img src="${mb.productPic}"></td>
              </tr>
            </tbody>
            <c:if test='${vs.last}'>
              <c:out value='</table>' escapeXml='false' />
            </c:if>
          </c:forEach>
        </c:if> 



reply to postreply to post
» JWorld@TW »  Object Relational Mapping » Hibernate

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8