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

"Christopher Dodunski" <[email protected]>
Newsgroups gmane.comp.java.tapestry.user
Message-ID <[email protected]>
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: [email protected]
For additional commands, e-mail: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.