Wicket 6 NonCachingImage未以HTML格式显示 [英] Wicket 6 NonCachingImage not displayed in HTML

查看:119
本文介绍了Wicket 6 NonCachingImage未以HTML格式显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很难过这个:
这是我创建动态图像的方法。

I'm really stumped with this one: This is my method to create a dynamic image.

 private Image createImage(final String id, final byte[] imageData){
          NonCachingImage chartImage=new NonCachingImage(id) {
            private static final long serialVersionUID = 1L;

            @Override
            protected IResource getImageResource() {
                return new DynamicImageResource(){
                    private static final long serialVersionUID=1L;                  
                    @Override
                    protected byte[] getImageData(Attributes attributes) {  
                        String myImageStr = new StringBuffer(
                                WicketApplication.TOMCAT_IMAGE_DOC_BASE).
                                append("13835.jpg").toString();
                        File file = new File(myImageStr);
                        try {
                            return Files.readBytes(file);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        return null;
                    }
                };
            }

          };
          chartImage.setOutputMarkupId(true);
          chartImage.setOutputMarkupPlaceholderTag(true);
          return chartImage;
    }

这是我使用它的地方

图片img = createImage(orig_photo,uploadedFile.getBytes());
pnlForm.addOrReplace(img);

Image img = createImage("orig_photo", uploadedFile.getBytes()); pnlForm.addOrReplace(img);

我的HTML

My HTML

<img width="100" height="133" title="Photo" wicket:id="orig_photo"/>

获取此错误:

在Javascript Inspect元素中获取此错误:
GET https:// localhost / admin / admnwtxtprofile?5-IResourceListener-wProfileTxtPnl-wProfileTxtForm-orig_photo& antiCache = 1439492929071 404(不找到了

Get this error in Javascript Inspect Element: GET https://localhost/admin/admnwtxtprofile?5-IResourceListener-wProfileTxtPnl-wProfileTxtForm-orig_photo&antiCache=1439492929071 404 (Not Found)

推荐答案

您已将imageData提供给您的createImage,但之后未在方法内使用。
我假设你的uploadedFile来自某个文件夹?可能是临时的?

You have imageData supplied to your createImage but then not used inside method. I assume your uploadedFile is from some folder? May be temp?

下面的代码与你的相似'工作正常,显示图像。

The code below similar to yours' is working fine, showing the image.

public class WicketApplication extends WebApplication {

    public static final String LOCAL_IMAGE_BASE = "c:\\myimages\\";
... 
}

public class TestDynaImageFilePage extends WebPage {

    public TestDynaImageFilePage(PageParameters parameters) {
        super(parameters);
        Form<Void> form = new Form<Void>("imageForm");
        form.setOutputMarkupId(true);

        add(form);

        addFormComponents(form);
    }

    private void addFormComponents(Form<Void> form) {
        Path path = Paths.get("src/main/webapp/images/help.png");
        try {
            byte[] data = java.nio.file.Files.readAllBytes(path);
            Image img = createImage("orig_photo", data);
            form.addOrReplace(img);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private Image createImage(final String id, final byte[] imageData) {
        NonCachingImage chartImage = new NonCachingImage(id) {
            private static final long serialVersionUID = 1L;

            @Override
            protected IResource getImageResource() {
                return new DynamicImageResource() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    protected byte[] getImageData(Attributes attributes) {
                        String myImageStr = new StringBuffer(
                                WicketApplication.LOCAL_IMAGE_BASE).
                                append("help.png").toString();
                        File file = new File(myImageStr);
                        try {
                            return Files.readBytes(file);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        return null;
                    }
                };
            }

        };
        chartImage.setOutputMarkupId(true);
        chartImage.setOutputMarkupPlaceholderTag(true);
        return chartImage;
    }
}

HTML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:wicket>
<head>

</head>
<body>
<form wicket:id="imageForm">
    <img width="100" height="133" title="Photo" wicket:id="orig_photo"/>
</form>
</body>
</html>

这篇关于Wicket 6 NonCachingImage未以HTML格式显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆