[英]When I use o:graphicImage, the image is not displayed
我無法顯示數據庫中的圖像,它們被存儲為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>
我正在使用基於BalusC代碼: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”屬性的消息
使用螢火蟲,我只會在每一行上看到這一點:
<img alt="Imagen no encontrada" src="/patientdiagnostics/javax.faces.resource/ImageStreamer_getById.xhtml?ln=omnifaces.graphic&v=0&p=7">
我還嘗試了類似於<ui:repeat>中<p:graphicImage>中的Display database blob images的操作。
我可以毫無問題地將所有圖像存儲在數據庫中。
因此,這實際上是不正確的。 這些圖像似乎是空的或僅填充零。
如何從<p:fileUpload>
抓取上傳的文件內容,請轉到以下答案:
與具體問題無關 ,由於圖像請求不代表JSF頁面,您要在流光的catch塊中添加的面對消息將不會出現在任何地方。 您最好只記錄/郵寄它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.