< p:graphicImage>不渲染图像 [英] <p:graphicImage> not rendering image

查看:71
本文介绍了< p:graphicImage>不渲染图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要显示图像的JSF页面.图像作为斑点存储在数据库中. 实体看起来像这样:

I have a JSF page where I want to show a image. The image is stored in the database as a blob. The entity looks like this:

@Entity
public class Player
{
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long            id;
    @Lob
    private byte[]          pictureData;
    @Transient
    private StreamedContent streamedPicture;

    public StreamedContent getStreamedPicture()
    {
        if (streamedPicture == null && pictureData != null)
        {
            try
            {
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                os.write(pictureData);
                streamedPicture = new DefaultStreamedContent(
                                                    new ByteArrayInputStream(
                                                                                os.toByteArray()),
                                                    "image/png");
            }
            catch (FileNotFoundException e)
            {}
            catch (IOException e)
            {}
        }
        return streamedPicture;
    }
}

JSF页面是这样的:

The JSF page is this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<h:head></h:head>
<body>
    <ui:repeat var="player" value="#{playerbean.cachedPlayers}">
        <h:outputText value="#{player.id}" />
        <p:graphicImage value="#{player.streamedPicture}" rendered="#{player.streamedPicture != null}"/>
    </ui:repeat>
</body>
</html>

我调用的bean看起来像这样:

And the bean I call looks like this:

@ManagedBean(name = "playerbean")
@SessionScoped
public class PlayerBean
        implements Serializable
{
    @EJB
    private PlayerManager   playerManager;
    private List<Player>    cachedPlayers;

    public List<Player> getCachedPlayers()
    {
        if (cachedPlayers == null)
        {
            cachedPlayers = playerManager.getAll();
        }
        return cachedPlayers;
    }
}

在调试时,我在方法handleResourceRequest()中的PrimeResourceHandler中设置了一个断点.我正在查看的PrimeResourceHandler的代码包含以下内容:

While debugging I set a breakpoint in PrimeResourceHandler in the method handleResourceRequest(). The code of the PrimeResourceHandler I'm looking at contains this:

try {
    String dynamicContentEL = (String) session.get(dynamicContentId);
    ELContext eLContext = context.getELContext();
    ValueExpression ve = context.getApplication().getExpressionFactory().createValueExpression(context.getELContext(), dynamicContentEL, StreamedContent.class);
    StreamedContent content = (StreamedContent) ve.getValue(eLContext);
    HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();

    response.setContentType(content.getContentType());

    byte[] buffer = new byte[2048];

    int length;
    InputStream inputStream = content.getStream();
    while ((length = (inputStream.read(buffer))) >= 0) {
        response.getOutputStream().write(buffer, 0, length);
    }

    response.setStatus(200);
    response.getOutputStream().flush();
    context.responseComplete();

} catch(Exception e) {
    logger.log(Level.SEVERE, "Error in streaming dynamic resource.");
} finally {
    session.remove(dynamicContentId);
}

通过时,行StreamedContent content = (StreamedContent) ve.getValue(eLContext); content似乎为空.当然,这会导致NullPointerException.但是,在JSF页面中,我告诉元素如果值为null则不要渲染.

When passing the line StreamedContent content = (StreamedContent) ve.getValue(eLContext); content appears to be null. This of course causes a NullPointerException. However in the JSF page I told the element not to render if the value is null.

推荐答案

<p:graphicImage>组件不能具有指向SessionScoped bean中托管属性的value属性.该值必须设置为RequestScoped bean.

The <p:graphicImage> component cannot have a value attribute that points to a managed property within a SessionScoped bean. The value must be set to a RequestScoped bean.

这是因为对image/jpeg内容类型HTTP响应的HTTP请求本质上是无状态的.浏览器将对JSF页面内容进行初始请求,然后对于呈现的每个HTML <img>标签,它将分别对每个<img>标签的动态生成的URL进行单独的请求以获取这些内容.在有状态的上下文中获取图像实际上没有任何意义.

This is because an HTTP request for an image/jpeg content type HTTP response is inherently stateless. The browser will make the initial request for the JSF page content, then for every HTML <img> tag that is rendered it will make seperate requests to the dynamically generated url of each <img> tag to fetch these. Fetching an image in a stateful context doesn't really make sense.

这篇关于&lt; p:graphicImage&gt;不渲染图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆