wood burning stoves 2.0
The moose likes JSF and the fly likes render myimage  from backing bean method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "render myimage  from backing bean method" Watch "render myimage  from backing bean method" New topic

render myimage from backing bean method

Susan Monz

Joined: Aug 23, 2008
Posts: 27

I deal with JSF with ADF BC as my model layer and i work on Jdeveloper

My Requirement:

My initial task was to display my blob image on my jsf. I did this by calling a method in my backing bean displayImage() which has the following code
ccdphotoAMImpl ccdam = (ccdphotoAMImpl) ADFUtils.getApplicationModule("ccdphotoAM");
ViewObjectImpl vo = ccdam.getvwCcdPhoto1();
//get my viewobject set the parameters for the query and execute
vo.setNamedWhereClauseParam("partyRefNo", partyRef);
if(vo.first()!= null){
System.out.println("vo.first() ");
BlobDomain imageFile = (BlobDomain) vo.getCurrentRow().getAttribute("ImageData");
FileOperations.displayFile(imageFile) is another helper class that has a method which acts like the servlet doGet method.

Now what happens is my image gets loaded in a new page which has the following url.

http://ip-address: /ccdphoto-ViewController-context-root/faces/displayPhoto.jspx;jsessionid=c0a8663d231da3d659987d034f81b899d8633fb2e6a2
which is similar to my initial page.
http://ip-address: /ccdphoto-ViewController-context-root/faces/displayPhoto.jspx

I want my image to be displayed in the original page in an image component
<!--h:graphicImage url="xyz" value = ""/-->

But i don't know how to call my backing bean method in this url.

Please help....
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You can't do it that way. Rather write an independent servlet which takes some parameters (partyRef?) and writes the desired image to the outputstream of the response based on the parameters.
Susan Monz

Joined: Aug 23, 2008
Posts: 27
Thanks for responding. Thats what they have tried here before when they used JSP+Struts+ ADFBC. Now we are using JSF, for which I was hoping there would be a way where i could avoid the servlet and call it through my backing bean. I was successful in getting my image but isn't there a way i can get my displayFile() method to redirect the image to my desired jsp. Perhaps somehow i could mention it in my faces-config.
HttpServletResponse response = (HttpServletResponse) extContext.getResponse();

response.setContentLength((int) length.intValue());
// response.setHeader("Pragma","no-cache");
// response.setHeader("Cache-Control","no-cache, post-check=0, pre-check=0");
//response.setHeader("Cache-Control","no-store" );
response.setDateHeader("Expires", 0);
try {
InputStream in = blobDomain.getBinaryStream();
OutputStream out = response.getOutputStream();

byte[] buf = new byte[1024];
int count;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);


I am sorry if i am pressing it. I was just hoping JSF had a way to do it.

Susan Monz

Joined: Aug 23, 2008
Posts: 27
One more thing, my collegue suggested I can return the response to a field in the backing bean and perhaps typecast it to a field in the bean.

Is this possible!
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Let's go in detail:

The h:graphicImage renders nothing else than a simple HTML <img> element. The value attribute of the h:graphicImage renders nothing else than the 'src' attribute of the <img> element. The HTML specification states that the 'src' attribute should point to a valid URL resource.

JSF can't do any magic to let it accept a binary stream, so that you could let the getter of the h:graphicImage value return a byte[] or so. You can also not let JSF create two response streams automagically, one for the page and another for the image. This makes all no sense.

You need to let it point to a valid URL resource somehow. The best and cleanest approach is to write a Servlet which does the desired task and call it in the h:graphicImage value.
Susan Monz

Joined: Aug 23, 2008
Posts: 27
Right! Ok will do. Thanks for making it clear. What i have done is as you said written a servlet which i call in my image tag

<af: objectImage source = "/imageServlet?id=#{myBean.tempID}"
rendered = "false"

have a text field to allow user to enter his employee id ( this case his id) and with ValueChangeListener = #{myBean.someaction}
populate this value into a field in myBean .Also I have a button in my jspx which on click will call a method in my backing bean will take this id and also set my objectImage1 rendered = "true"
So now my image get populated within the same page and when i want.

The reason i hesitated in using the servlet was coz i didn't know how to get the id which got populated in the very same page. Silly I know! But thank you.
I agree. Here's the link: http://aspose.com/file-tools
subject: render myimage from backing bean method
It's not a secret anymore!