如何使用 Hibernate 在 Struts 2 中的 mySql DB 的 jsp 页面中显示图像(bolb 类型)

How to display image(bolb type) in jsp page from mySql DB in Struts 2 using Hibernate(如何使用 Hibernate 在 Struts 2 中的 mySql DB 的 jsp 页面中显示图像(bolb 类型))
本文介绍了如何使用 Hibernate 在 Struts 2 中的 mySql DB 的 jsp 页面中显示图像(bolb 类型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临一个问题,如何使用 Hibernate 在 Struts 2 中的 mySql DB 中在 jsp 页面中显示图像(bolb 类型)?请分享您的观点.提前致谢.

I am facing one problem, How to display image(bolb type) in jsp page from mySql DB in Struts 2 using Hibernate ?Please share your view.Thanks in advance.

    public byte[] getrepImagechange2(int loginid) {

    Criteria criteria = null;
    byte[] repCurrentImage = null;

    try {
        session = sessionFactory.openSession();
        criteria = session.createCriteria(Membersdetails.class).add(Expression.eq("logintable.loginId", loginid));
        List list = criteria.list();
        Iterator itr = list.iterator();
        if (itr.hasNext()) {

            Membersdetails get = (Membersdetails) itr.next();
            repCurrentImage = get.getRepPicture();

            HttpServletResponse response23 = ServletActionContext.getResponse();
            response23.setContentType("image/jpg");
            OutputStream out = response23.getOutputStream();
            out.write(repCurrentImage);
            out.close();

        }
    } catch (Exception e) {
        System.out.println("Exception in getrepImage() :" + e);
    } finally {
        try {

            session.flush();
            session.close();
        } catch (Exception e) {
            System.out.println("Exception in getrepImage resource closing  :" + e);
        }
    }
    return repCurrentImage;
}
  And I am displaying this image in jsp page in a table cell using this code :                     
   <img src="<s:property value="bs"/>" 

推荐答案

我使用以下从 JPA (Hibernate Backed) 渲染图像的示例使用 struts2-conventions-plugin,结果类型注释stream"全部存在是视图:

I use the following to render images from JPA (Hibernate Backed) example uses the struts2-conventions-plugin, in the result type annotation "stream" is all there is to the view:

package com.kenmcwilliams.photogallery.action.gallery;

import com.kenmcwilliams.photogallery.orm.Picture;
import com.kenmcwilliams.photogallery.orm.PictureDetails;
import com.kenmcwilliams.photogallery.service.Gallery;
import com.opensymphony.xwork2.ActionSupport;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

@Result(type = "stream", params = {
    "contentType", "${contentType}",
    "contentLength", "${contentLength}",
    "contentDisposition", "${contentDisposition}",
    "inputStream", "${inputName}",
    "bufferSize", "${bufferSize}",
    "allowCaching", "${allowCaching}"
})
public class Stream extends ActionSupport {
    @Autowired private Gallery gallery; 
    private String contentType = "text/plain";
    private int contentLength = 0;
    private String contentDisposition = "inline";
    private InputStream inputStream;
    public String inputName = "inputStream";//This should not be required
    private Integer bufferSize = 1024;
    private String allowCaching = "true";
    private Integer id = null;

    @Override
    public String execute() {
        if (id != null){
            //gallery.get
            PictureDetails details = gallery.getPictureDetails(id);
            Picture photo = details.getPictureId();
            this.contentType = details.getContentType();
            System.out.println("Content Type: " + contentType);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(photo.getPicture());
            this.contentLength = photo.getPicture().length;
            System.out.println("Content Length: " + contentLength);
            this.inputStream = byteArrayInputStream;
        }else{
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * @return the contentType
     */
    public String getContentType() {
        return contentType;
    }

    /**
     * @param contentType the contentType to set
     */
    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    /**
     * @return the contentLength
     */
    public int getContentLength() {
        return contentLength;
    }

    /**
     * @param contentLength the contentLength to set
     */
    public void setContentLength(int contentLength) {
        this.contentLength = contentLength;
    }

    /**
     * @return the contentDisposition
     */
    public String getContentDisposition() {
        return contentDisposition;
    }

    /**
     * @param contentDisposition the contentDisposition to set
     */
    public void setContentDisposition(String contentDisposition) {
        this.contentDisposition = contentDisposition;
    }

    /**
     * @return the bufferSize
     */
    public int getBufferSize() {
        return bufferSize;
    }

    /**
     * @return the allowCaching
     */
    public String getAllowCaching() {
        return allowCaching;
    }

    /**
     * @param allowCaching the allowCaching to set
     */
    public void setAllowCaching(String allowCaching) {
        this.allowCaching = allowCaching;
    }

    /**
     * @return the inputStream
     */
    public InputStream getInputStream() {
        return inputStream;
    }

    /**
     * @param inputStream the inputStream to set
     */
    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }
}

您还询问了如何显示上述内容,以下是用于显示图片库的 JSP(因此该操作将为该 JSP 提供图像 id,上述操作将使用该图像 id 从数据库和图库的标题).

You also asked about how to display the above, the following is a JSP used to show a gallery of pictures (so the action will provide this JSP with image ids which the above action will use to get the pictures from the DB and the title of the Gallery).

如果我没记错的话,这个画廊会显示四张图片,其中包含显示所有图片所需的行数.

If I remember correctly this gallery displays four pictures wide with as many rows as needed to show all the pictures.

<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1><s:property value="photoGallery.name"/></h1>
        <table>
            <s:iterator begin="0" end="pictureDetails.size/4" var="row">
                <tr>
                    <s:subset source="pictureDetails" start="4 * #row" count="4">
                        <s:iterator>
                            <s:url forceAddSchemeHostAndPort="true" namespace="/gallery" action="stream" var="streamURL">
                                <s:param name="id" value="id"/>
                            </s:url>
                            <td>
                                <s:a value="%{#streamURL}"><img width="200px" src="<s:property value="#streamURL"/>"/></s:a>
                            </td>
                        </s:iterator>
                    </s:subset>
                </tr>
            </s:iterator>
        </table>
    </body>
</html>

在上面这行可能是这部分可能是你想要的:

In the above this line is probably this part is probably what you want:

<img width="200px" src="<s:property value="#streamURL"/>"/>

这篇关于如何使用 Hibernate 在 Struts 2 中的 mySql DB 的 jsp 页面中显示图像(bolb 类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)