0

ページ:

<ui:repeat value="#{testController.photos}" var="item">
    <p:graphicImage value="#{item}"/>
</ui:repeat>

豆:

private TestEntity current;

public List getPhotos() {
    StreamedContent image;
    Collection rawPhotoCollection = current.getPhotoCollection();

    List<StreamedContent> imageList = new ArrayList<StreamedContent>(rawPhotoCollection);
    List<Photo> photoList = new ArrayList<Photo>(rawPhotoCollection);

    for (int i = 0; i < photoList.size(); i++) {
        byte[] data = photoList.get(i).getImage();
        ByteArrayInputStream is = new ByteArrayInputStream(data);
        image = new DefaultStreamedContent(is, "image/png");
        imageList.set(i, image);
    }

    return imageList;
}

TestEntity には Photo Entity のコレクションがあります。写真コレクションを写真の配列リストにキャストしました。Photo には、BLOB として宣言された列 Image があります。各画像を取得するループを作成して、それを StreamedContent に変換できるようにしました。各 StreamedContent を imageList (StreamedContent のリスト) に挿入すると、imageList が返され、p:graphicImage として表示されました。

問題は、ページに壊れた画像のアイコンが表示されることです。画像は表示されません。ui:repeat を使用せず、p:graphicImage を使用して選択した写真エンティティを表示するだけで、正しい画像が表示されます。

何かのようなもの:

ページ:

<p:graphicImage value="#{testController.firstImage}"/>

豆:

public StreamedContent getFirstImage() {
    Collection rawPhotoCollection = current.getPhotoCollection();
    List<Photo> photoList = new ArrayList<Photo>(rawPhotoCollection);
    byte[] data = photoList.get(0).getImage();
    ByteArrayInputStream is = new ByteArrayInputStream(data);
    StreamedContent image = new DefaultStreamedContent(is, "image/png");
    return image;
}

上記のコードは機能します。

4

0 に答える 0