3

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;
    }
}

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>

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;
    }
}

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);
}

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.

4

1 回答 1

3

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

这是因为image/jpeg内容类型 HTTP 响应的 HTTP 请求本质上是无状态的。浏览器将对 JSF 页面内容发出初始请求,然后对于<img>呈现的每个 HTML 标记,它将对每个<img>标记的动态生成的 url 发出单独的请求以获取这些内容。在有状态的上下文中获取图像并没有真正的意义。

于 2012-07-12T11:59:22.090 回答