Re: How to display a Blob (byte[]) from database in a Tapestry template

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: How to display a Blob (byte[]) from database in a Tapestry template

Christopher Dodunski-4
Thank you for your help.  I corrected those two oversights, and got the
class working successfully by changing the onActivate() parameter from
byte[] to 'User' (code below).  The image now displays, but I wonder if
when passing a 'User' object Tapestry passes just the id as reference
(using Tapestry-Hibernate ValueEncoder), or whether I ought to explicitly
pass the id like so: user.getId().

Also, Welcome.tml will untimately display several images from the
database, of different entity types and multiple instances.  I'm wondering
how BlobImage.class might be called numerous times from the same template.

Finally, I tried your shorter alternative: "return
"data:image/png;base64," +
Base64.getUrlEncoder().encodeToString(user.getImage());".  It didn't work;
the compiler seemed to point to the Java version (I use 1.8).

Thanks,

Chris.


[CODE]
package com.example.harbour.pages;

import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.services.Response;

import com.example.harbour.entities.User;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Class created for displaying Blob (profile) images read from the
database, as per the below howto:
 *
https://stackoverflow.com/questions/13213236/tapestry-display-blob-using-markup
 */
public class BlobImage {

    @Property
    private User user;

    public StreamResponse onActivate(User user){

        this.user = user;

        //Retrieve your image using the context parameter(s)
        final InputStream imageStream = new
ByteArrayInputStream(user.getImage());

        return new StreamResponse(){

            @Override
            public InputStream getStream() throws IOException {
                return imageStream;
            }

            @Override
            public String getContentType(){
                return "image/png";
            }

            @Override
            public void prepareResponse(Response response){}
        };
    }
}
[/CODE]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]