jsf - 使用 p :graphicImage in a tagfile 显示 BLOB 图像

标签 jsf primefaces http-status-code-404 graphicimage tagfile

使用 <p:graphicImage> 显示 BLOB 图像如下。

<p:graphicImage value="#{categoryBean.image}">
    <f:param name="id" value="7"/>
</p:graphicImage>

哪里CategoryBean定义如下。

@Named
@ApplicationScoped
public class CategoryBean {

    @Inject
    private CategoryService service;

    public CategoryBean() {}

    public StreamedContent getImage() throws IOException {
        FacesContext context = FacesContext.getCurrentInstance();

        if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            return new DefaultStreamedContent();
        } else {
            String id = context.getExternalContext().getRequestParameterMap().get("id");
            byte[] bytes = Utils.isNumber(id) ? service.findImageById(Long.parseLong(id)) : null;
            return bytes == null ? null : new DefaultStreamedContent(new ByteArrayInputStream(bytes));
        }
    }
}

关于上述方法,以下自定义标签应该可以完美工作,但无法在 <p:graphicImage> 上显示图像没有错误/异常。

<my:image bean="#{categoryBean}" property="image" paramName="id" paramValue="7"/>

标记文件位于 /WEB-INF/tags/image.xhtml 下.

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:p="http://primefaces.org/ui"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:f="http://xmlns.jcp.org/jsf/core">

    <p:graphicImage value="#{bean[property]}">
        <f:param name="#{paramName}" value="#{paramValue}"/>
    </p:graphicImage>
</ui:composition>

生成的<img>标签看起来不错:

<img id="form:j_idt4"
     src="/ContextPath/javax.faces.resource/dynamiccontent.properties.xhtml?ln=primefaces&amp;v=5.3&amp;pfdrid=IA0%2F7ZuBnGS%2BSzeb%2BHyPOTo4Pxp4hjI6&amp;pfdrt=sc&amp;id=7&amp;pfdrid_c=true"
     alt=""/>

它仅返回 HTTP 404 错误。

给定的自定义标签的定义是否存在缺陷?

最佳答案

这是由 PrimeFaces <p:graphicImage> 的方式引起的识别图像请求。基本上,它转换精确值表达式 #{bean[property]}转换为字符串,对其进行加密,然后将其作为 pfdrid 传递值。当网络浏览器需要通过全新的 HTTP 请求下载图像时,该值表达式将在“当前”EL 上下文中进行解密和计算。然而,在那一刻,没有任何地方有#{bean}。也不#{property}在 EL 上下文中的任何地方都可用,因为没有办法使用带有标记文件和所有内容的 JSF View 。只有请求、 session 和应用程序范围的 bean 在 EL 上下文中可用。

除了向 PrimeFaces 人员报告问题之外,没有其他办法可以解决此问题。

至于替代解决方案,OmniFaces <o:graphicImage> 通过在渲染响应期间而不是在流式传输图像期间检查目标 bean/方法,可以在这方面做得更好。它立即检查 #{bean[property]} ,发现它实际上代表 #{categoryBean.image} ,然后就成功了。只是为了确保我在像您一样的标记文件中测试了它,它对我来说工作得很好,而 PF 确实按照描述失败了。

<o:graphicImage value="#{bean[property](paramValue)}" />

public byte[] getImage(Long id) throws IOException {
    return service.findImageById(id);
}

关于jsf - 使用 p :graphicImage in a tagfile 显示 BLOB 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35339701/

相关文章:

jsf - p :tooltip does not work as expected 的动态 "for"属性

java - 如何在 primefaces 列中水平对齐按钮?

java - 在 Primefaces5+JSF2 中渲染不工作

css - Primefaces 菜单栏垂直分隔符

tomcat - 使用 Webpack 将 Angular 2 应用程序部署到 Tomcat - 404 错误

asp.net-mvc - MVC View 在 IIS 7.5 中返回 404

jsf - 单击特定选项卡时调用 JavaScript

spring - 在 Spring+Tomcat 上使用 JSF 2.2.9 会导致 java.lang.NoClassDefFoundError : javax/enterprise/context/spi/Contextual

java - @PostConstruct 第一次执行失败

android - 在 Google Cloud Messaging 的用户通知中创建 notification_key 时出现 404 错误