【发布时间】:2016-05-19 19:37:00
【问题描述】:
我在显示我的数据库检索到的图像时遇到了一些问题。
查看调用者:
<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,javax.faces.bean.ApplicationScoped用于 JSF。 -
这不是范围问题
-
您的意思是说,CDI bean 在“常规”页面中可以正常工作?
-
@BalusC “常规”是什么意思?我有一个工作正常的 ViewScoped CDI bean。当然我不能将graphicimage与ViewScoped bean一起使用,所以我必须创建一个ApplicationScoped(或SessionScoped)bean......但它只适用于ManagedBean
-
如果可以的话,最好使用 Omnifaces 的 o:graphicImage。
标签: primefaces cdi jsf-2.2 managed-bean payara