sql >> Database teknologi >  >> RDS >> Mysql

Sådan vises billede (bolb type) i jsp-side fra mySql DB i Struts 2 ved hjælp af Hibernate

Jeg bruger følgende til at gengive billeder fra JPA (Hibernate Backed) eksempel bruger struts2-conventions-plugin, i resultattypen annotation "stream" er alt der er til visningen:

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;
    }
}

Du spurgte også om, hvordan man viser ovenstående, det følgende er en JSP, der bruges til at vise et galleri med billeder (så handlingen vil give denne JSP billed-id'er, som ovenstående handling vil bruge til at hente billederne fra DB'en og titlen på galleriet).

Hvis jeg husker rigtigt, viser dette galleri fire billeder bredt med så mange rækker som nødvendigt for at vise alle billederne.

<%@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>

I den ovenstående linje er sandsynligvis denne del er sandsynligvis, hvad du ønsker:

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



  1. Django models.py Cirkulær udenlandsk nøgle

  2. Sammenfletning af datafiler med Statistica, del 2

  3. Hvordan tilføjer man kommentarer i SQL?

  4. Python List til PostgreSQL Array