Wicket 6 NonCachingImage not displayed in HTML

风流意气都作罢 提交于 2019-12-12 01:28:51

问题


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;
    }

This is where I use it

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

My HTML

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

Get this error:

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


回答1:


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>



回答2:


I think this may have something to do with how Wicket request/response life cycle. I have the same code in BeforeRender method and it works fine but when I copy it in OnSubmit, it doesn't work. My solution was to refresh the page in OnSubmit instead of calling the above method.



来源:https://stackoverflow.com/questions/31996827/wicket-6-noncachingimage-not-displayed-in-html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!