Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying an image BLOB in my JSF page

 
nakatami stars
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello,

I am a beginner in JSF, and I wnat todisplay a picture loaded from my Data Base in my JSF page.

I follow this steps to get my image from my DataBase but I don't know how can I put it in my JSF page:

1/My image is stored in my Data base (Blob type)

2/I load this image from my database and Finally It stored it as a ObjectInputStream;

3/I don't know how can I display it in my JSF page because I don't know what I ll put in the url <h:graphicImage id="image" url="?"/>

please help me.
 
Brendan Healey
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you will find is that as a beginner it is going to be extremely difficult to do this. Do
not take these words lightly. Is this your primary objective or just one of the things you
have been tasked with?
 
nakatami stars
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replaying.

I am developping a project, so I decided to use JSF because I see that this freemwork make tasks easy. So I stoped when I faced this problem.

 
Brendan Healey
Ranch Hand
Posts: 218
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This code demonstrates the loading & display of a BLOB from a database using h:graphicImage
without any temporary files. Whilst not entirely generic you should be able to get this working
with a days work if you're prepared to stick at it.

The scenario is that you have an "Employee" JPA persistent entity that implements the
"PhotoInterface" interface that defines one method, getPhoto(). Given a database with a table
with an integer primary key, where the entity implements PhotoInterface this will display a
byte[] as an image on your page.

"yourEJB" is simply an EJB that has a find function which is EntityManager.find().

Replace or remove SQLog.log() with your logging approach.

In the example below we have an "Employee" entity that supports the PhotoInterface and
therefore has a getPhoto() function. The Employee class (@Entity) has a byte[]
photo property.

In this way you can retrieve images from any entity/table that supports PhotoInterface
given the primary key of the table row.







Regards,
Brendan.
 
nakatami stars
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
kikoo kikoooo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use a librairie component for jsf like primefaces and you can do it easy

you can find exemple here http://jeebestpractices.blogspot.com/
 
Brendan Healey
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It's not like I wrote the code for fun. The issue was that the PrimeFaces fileUpload component
was not working with container security, or on a page with more than one view parameter.

Cagatay has asked me to test the latest version which I will do at some point.

Brendan.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic