Reputation: 105
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)
Upvotes: 0
Views: 232
Reputation: 105
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.
Upvotes: 0
Reputation: 270
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>
Upvotes: 1