我有一些问题显示我的数据库检索到的图像。primefaces graphicimage CDI bean不起作用
查看来电者:
<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?
'javax.enterprise.context.ApplicationScoped'为CDI,'用于JSF的javax.faces.bean.ApplicationScoped'。 – Geinmachi
它不是范围问题 – Marco
你的意思是说,CDI bean在“常规”页面中工作正常吗? – BalusC