I have a database with some images. Could anyone explain me how I could load an image in a JSF page?
I already have a managed bean that converts an Image object into a streamcontent. This streamcontent is called from the page in a tag <h:graphicImage>
, but when I check the source code of the page, there's no src
where the image could be loaded.
The JSF
<h:graphicImage>
get rendered as a HTML<img>
element. Itssrc
attribute should point to an URL, not to the binary contents. So you should store the URL (or at least some identifier as request parameter or pathinfo) in the JSF bean and create a separate servlet to stream the image from the DB to the HTTP response.Use this in your JSF page:
Assuming that
bean.getImageId()
returns123
, this get rendered in HTML as:Create a
Servlet
class which is mapped inweb.xml
on anurl-pattern
of/images/*
and implement itsdoGet()
method as follows.:In the
ImageDAO#find()
you can useResultSet#getBinaryStream()
to the image as anInputStream
from the database.An extended example can be found in this article.