【问题标题】:primefaces graphicimage CDI bean doesn't workprimefaces graphicsimage CDI bean不起作用
【发布时间】: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


【解决方案1】:

检查您是否在导入中有 javax.enterprise.context.ApplicationScoped

如果您对 @ApplicationScoped 有不同的导入(例如 javax.faces.bean.ApplicationScoped),那么您需要配置 CDI 以发现所有 bean,而不是仅发现带有 CDI 注释的那些(这是默认设置)

要调整所有 bean 的发现,请将空的 beans.xml 添加到 WEB-INF 目录中,或者如果您已经有 beans.xml,请将 bean-discovery-mode="all" 添加到 &lt;beans&gt; 元素中,如下所示:

<?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>

【讨论】:

  • 你完全正确!所以问题是关于 CDI 实现(enterprise.contexr vs faces.beans)你能把我链接到我可以更好地研究 CDI 的地方吗?非常感谢
  • 对于一些注解,你确实需要注意包名。 javax包中有注解双打,同名但包不同,因此含义不同。例如。 @Singleton@Singleton。它们来自不同的规格,因此不能混用。
  • 您可以在此页面找到更多关于 CDI 的信息:cdi-spec.org,特别是关于 bean 发现的信息:docs.jboss.org/cdi/spec/1.2/cdi-spec.html#type_discovery_steps
猜你喜欢
  • 2015-07-26
  • 1970-01-01
  • 2011-12-25
  • 2023-03-07
  • 1970-01-01
  • 2017-09-30
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
相关资源
最近更新 更多