Book Image

Primefaces Cookbook Second Edition

Book Image

Primefaces Cookbook Second Edition

Overview of this book

Table of Contents (20 chapters)
PrimeFaces Cookbook Second Edition
Credits
Foreword
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Creating dynamic image streaming programmatically


The graphicImage component can also render an image that is created programmatically in the server-side backing bean.

How to do it…

The following is an example that renders a PrimeFaces logo, which is read with the resource streaming mechanism:

<p:graphicImage value="#{dynaImageBean.graphicText}" />

public StreamedContent getGraphicText() throws IOException {
  InputStream stream =this.getClass()
    .getResourceAsStream("/chapter7/primefaces.jpg");
  return new DefaultStreamedContent(stream);
}

How it works...

As seen, the getGraphicText() method returns an instance of StreamedContent. PrimeFaces also provides a default implementation for the stream content, which is org. primefaces.model.DefaultStreamedContent. The backing bean containing the graphicText getter method should be defined in the session scope. The reason behind this is that the image will be fetched in a separate request from the rest of the page content, and in order to...