当前位置: 动力学知识库 > 问答 > 编程问答 >

image - tapestry display blob using markup

问题描述:

I have saved blob image in db, using this component I wish to embed blob from mysql to tag in tapestry.

I have found this on the net.

Do not understand how writer.element works

I think something like this should work

 @SupportsInformalParameters

public class DBImage {

@Parameter(required = true)

private Image image;

@Inject

private ComponentResources resources;

boolean beginRender(MarkupWriter writer,byte[] array){

writer.element("img", "src", "data:image/png;base64,"+array,);

resources.renderInformalParameters(writer);

writer.end();

return false;

}

}

than call the component every on page class

@Property

private DBImage image.beginRedner(entity.getBlobImage);

网友答案:

I haven't tried to use the data uri scheme with images in tapestry - i'm sure it 'can' work, but to call the you'd just include this in your tml:

<t:DBImage image="${entity.BlobImage}" />

and within your DBImage component you'll need a way to convert the image parameter into a byte array.

I don't want to take the time to create a working example of this technique, but i'll take you through how I solve this type of problem:

Just like with any image render within an html document, there MUST be two separate requests (unless using the data uri scheme):

  1. the page's html to render the image tag (ie http://host/context/app/mypage)
  2. the image data (ie http://host/context/app/myimage)

so for request one, you want to construct the image url and put it in an img tag:

<html>
...
<img src="/context/app/myimage/params" />
...
</html>

and request two will just return the byte stream of the image data.

Lets start with the second request, because it makes more sense that way.

In tapestry, an individual request is handled by creating a page class and optionally tml template. To serve a byte stream, you just need the page class. To pass parameters or context to the page, you use context params.

package myproject.pages;

public class MyImage
{
    public StreamResponse onActivate(String parameter)
    {
        // retrieve your image using the context parameter(s)
        final InputStream imageStream = getImage(parameter);

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

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

            @Override
            void prepareResponse(Response response)
            {}
        };
    }
}

At this point, you can request/render your image using the url "http://host/context/app/myimage/parameter". Now you just need to include this in your img tag, ie request 1.

So you'll need to generate the link first, which needs to be done in your page class:

package myproject.pages;

public class MyPage
{
    @Inject
    private PageRenderLinkSource pageLink;

    public Link getImageLink()
    {
        return pageLink.createPageRenderLinkWithContext(MyImage.class, parameter);
    }
}

All thats left is to use this link in your tml:

<img src="${imageLink}" />

Obviously you need to wire up your parameter and retrieval.

分享给朋友:
您可能感兴趣的文章:
随机阅读: