primefaces graphicimage CDI bean 不工作
primefaces graphicimage CDI bean doesn't work
我在显示我的数据库检索到的图像时遇到了一些问题。
查看来电者:
<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
导入。
如果您对 @ApplicationScoped
进行了不同的导入(例如 javax.faces.bean.ApplicationScoped
),那么您需要配置 CDI 以发现所有 bean,而不是仅发现带有 CDI 注释的那些(这是默认设置)
要调整所有 bean 的发现,请将空的 beans.xml
添加到 WEB-INF 目录中,或者如果您已经有 beans.xml,请将 bean-discovery-mode="all"
添加到 <beans>
元素,像这样:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="annotated">
</beans>
我在显示我的数据库检索到的图像时遇到了一些问题。
查看来电者:
<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
导入。
如果您对 @ApplicationScoped
进行了不同的导入(例如 javax.faces.bean.ApplicationScoped
),那么您需要配置 CDI 以发现所有 bean,而不是仅发现带有 CDI 注释的那些(这是默认设置)
要调整所有 bean 的发现,请将空的 beans.xml
添加到 WEB-INF 目录中,或者如果您已经有 beans.xml,请将 bean-discovery-mode="all"
添加到 <beans>
元素,像这样:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="annotated">
</beans>