当我使用o:graphicImage时,图像不会显示

时间:2015-05-04 13:39:15

标签: jsf jsf-2.2 omnifaces graphicimage

我无法显示数据库中的图像,它们存储为 bytea ,我将它们映射为:

@Entity
@Table(name = "photograph", schema = "public")
public class Photograph{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "photograph_id", unique = true, nullable = false)
    private Long id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "diagnostic_id", nullable = false, insertable = false, updatable = false)
    private Diagnostic diagnostic;

    @Column(name = "photo", nullable = false)
    private byte[] photo;

    @Column(name = "photograph_description", nullable = false, length = 100)
    private String photographDescription;

    @Column(name = "photograph_content_type")
    private String photographContentType;

//Getters and Setters...
}

我可以毫无问题地将所有图像存储在数据库中。问题是当我想在 p:dataTable 中显示它们时,这样:

<p:dataTable id="dataTableLoadedPhotos"
                value="#{imageController.photographListUpdate}" var="image">
                <p:column headerText="Fotografías cargadas" width="110">
                    <o:graphicImage value="#{imageStreamer.getById(image.photographId)}"
                        alt="#{msgs['label.diagnostic.photograph.notFound']}" />
                </p:column>
            </p:dataTable>

我正在使用基于The BalusC Code: ImageServlet流媒体,我尝试使用o:graphicImage但没有成功,mi代码中缺少某些内容:

@ManagedBean
@ApplicationScoped
public class ImageStreamer {

@EJB
private PhotographService photographService;

public byte[] getById(Long id) {
    try {
        return photographService.getContent(id);
    } catch (ServiceException e) {
        FacesMessage mensaje = new FacesMessage(
                FacesMessage.SEVERITY_ERROR,
                "Error al buscar la fotografía ", e.getMessage());
        FacesContext.getCurrentInstance().addMessage(null, mensaje);
    }
    return null;
}
}

我还有一个带有@RequestScoped的托管bean:

@ManagedBean
@RequestScoped
public class ImageController {

@EJB
private PhotographService photographService;

@ManagedProperty(value = "#{diagnosticDataManager}")
private DiagnosticDataManager diagnosticDataManager;

private List<Photograph> photographListUpdate = new ArrayList<Photograph>();
private Photograph selectedPhoto;

/**
 * 
 */
public ImageController() {
    diagnosticDataManager = new DiagnosticDataManager();
}

@PostConstruct
public void init() {
    if (diagnosticDataManager.getDiagnostic().getDiagnosticId() != null)
        photographListUpdate = photographService
                .findPhotosByDiagnostic(diagnosticDataManager
                        .getDiagnostic());

    for (Photograph photograph : photographListUpdate) {
        byte[] imageContent = org.apache.commons.codec.binary.Base64
                .decodeBase64(photograph.getPhoto());
        ExternalContext ec = FacesContext.getCurrentInstance()
                .getExternalContext();
        ec.getSessionMap()
                .put(photograph.getId().toString(),
                        imageContent);
    }
}
// Getters and setters....
}

我只看到p中的两行:dataTable对应于预先加载没有图像的图像,只显示o:graphicImage的“alt”属性消息

使用firebug我只在每一行上看到这个:

<img alt="Imagen no encontrada" src="/patientdiagnostics/javax.faces.resource/ImageStreamer_getById.xhtml?ln=omnifaces.graphic&v=0&p=7">

我也尝试过类似Display database blob images in <p:graphicImage> inside <ui:repeat>

的内容

1 个答案:

答案 0 :(得分:0)

  

我可以毫无问题地将所有图像存储在数据库中。

因此实际上并非如此。这些图像似乎是空的或仅填充零。

如何从<p:fileUpload>获取上传的文件内容,请转到以下答案:

无关具体问题,因为图片请求不在任何地方,因此您尝试在流媒体的catch块中添加的消息不会出现在任何地方。 t代表一个JSF页面。你最好只记录/邮寄它。