使用 Tapestry5 在我的应用程序外部显示图像

发布于 2024-12-09 01:24:35 字数 1108 浏览 4 评论 0原文

我正在使用 Tapestry 开发我的第一个项目,除了图像之外,我即将完成。

我想要什么?我只需要在应用程序外部显示图像,例如:/ home/app/images/image.jpg

我尝试了什么? 我一直在“谷歌搜索”并阅读 Tapestry5 论坛,我发现了这个:http://wiki.apache.org/tapestry/Tapestry5HowToStreamAnExistingBinaryFile

我按照步骤操作,创建类,但我需要要在另一个页面上显示嵌入的图像(因此我无法使用 ImagePage),我尝试了以下操作:

在页面 java 类

public StreamResponse getImage() {
        InputStream input = DetallesMultimedia.class
                .getResourceAsStream("/home/santi/Escritorio/evolution-of-mario.jpg"); //On application, i will retrieve this from DB
    return new JPEGInline(input,"hellow");
}

在页面上template

...
<img src="${image}" alt:image/>
...

or

...
${image}
...

显然,这不起作用,我真的不知道该怎么做。我读到了有关在事件上加载图像的信息(在该事件上返回 OutputStream,如上面链接的 HowTo 中所述),但我的英语很差(我相信您已经注意到)并且我不太明白我该如何这样做。

你能帮我一下吗?

谢谢大家。

I am developing my first project with Tapestry and I am about to finish, except for the images..

What do I want? I just need to display an image outside my application, example: /home/app/images/image.jpg

What did I try? I have been "googling" and reading Tapestry5 forums, I found this: http://wiki.apache.org/tapestry/Tapestry5HowToStreamAnExistingBinaryFile

I followed the steps, creating classes but I need to display the image embed on another page (so I can't use ImagePage), I tried this:

On page java class

public StreamResponse getImage() {
        InputStream input = DetallesMultimedia.class
                .getResourceAsStream("/home/santi/Escritorio/evolution-of-mario.jpg"); //On application, i will retrieve this from DB
    return new JPEGInline(input,"hellow");
}

On page template

...
<img src="${image}" alt:image/>
...

or

...
${image}
...

Obviusly, this didn't work and I really don't know how can I do it. I read about loading the image on an event (returning the OutputStream on that event, as it's said in the HowTo linked above) but my english is so bad (I am sure you already noticed) and I don't understand well how can I do that.

Could you help me please?

Thanks you all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昔梦 2024-12-16 01:24:35

我从未见过维基页面上的示例。下面是一些关于如何使用 StreamResponse 在类路径上加载图像的代码。

@Inject
private ComponentResources resources;

@OnEvent(value = "GET_IMAGE_STREAM_EVENT")
private Object getProfilePic() throws Exception {


    InputStream openStream = DetallesMultimedia.class.getResourceAsStream("/home/santi/Escritorio/evolution-of-mario.jpg");
    byte[] imageBytes = IOUtils.toByteArray(openStream);
    final ByteArrayInputStream output = new ByteArrayInputStream(imageBytes);

    final StreamResponse response = new StreamResponse() {

        public String getContentType() {
           "image/jpegOrPngOrGif";
        }

        public InputStream getStream() throws IOException {
            return output;
        }

        public void prepareResponse(Response response) {
            // add response headers if you need to here
        }

    };
    return response;
}

public String getPicUrl() throws Exception {
    return resources.createFormEventLink("GET_IMAGE_STREAM_EVENT");
}

在您的模板中:

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

I've never seen the examples as on the wiki page. Below some code on how to load an image on the classpath though using a StreamResponse.

@Inject
private ComponentResources resources;

@OnEvent(value = "GET_IMAGE_STREAM_EVENT")
private Object getProfilePic() throws Exception {


    InputStream openStream = DetallesMultimedia.class.getResourceAsStream("/home/santi/Escritorio/evolution-of-mario.jpg");
    byte[] imageBytes = IOUtils.toByteArray(openStream);
    final ByteArrayInputStream output = new ByteArrayInputStream(imageBytes);

    final StreamResponse response = new StreamResponse() {

        public String getContentType() {
           "image/jpegOrPngOrGif";
        }

        public InputStream getStream() throws IOException {
            return output;
        }

        public void prepareResponse(Response response) {
            // add response headers if you need to here
        }

    };
    return response;
}

public String getPicUrl() throws Exception {
    return resources.createFormEventLink("GET_IMAGE_STREAM_EVENT");
}

In your template:

<img src="${picUrl}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文