我在显示我的数据库检索到的图像时遇到了一些问题。
查看来电者:
<p:graphicImage value="#{appController.image}" height="200 px" >
<f:param name="oid" value="#{item.oid}" />
</p:graphicImage>
控制器:
@Named("appController")
@ApplicationScoped
public class AppController {
@Inject
private MultimediaFacade multimediaFacade;
public StreamedContent getImage() throws IOException {
System.out.println("getting image")
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
// So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL.
return new DefaultStreamedContent();
} else {
// So, browser is requesting the image. Return a real StreamedContent with the image bytes.
String imageId = context.getExternalContext().getRequestParameterMap().get("oid");
int oid=Integer.parseInt(imageId);
System.out.println(oid);
Multimedia image = multimediaFacade.find(oid);
System.out.println(Arrays.toString(image.getFileBlob()));
return new DefaultStreamedContent(new ByteArrayInputStream(image.getFileBlob()));
}
}
}
这段代码什么也没显示,看起来该方法从未被调用(从不在控制台中打印)!
经过几天的尝试更改范围后,我尝试使用@ManagedBean 而不是@Named,它有效!!!
有人可以解释一下为什么这仅适用于@ManagedBean 而不适用于@Named?